I'm building a component in a server application that manages connections between my application and an external service. My component detects when the server is unavailable, starts a background thread until the service becomes available, then re-enters a "working" state.
My problem is that if the process calling the component exits while this thread is running, the application won't terminate until the thread exits. Basically, my application can't shut down if this service is available.
My question: is there some way to detect if the application is trying to close so that any background threads can exit? I've noticed a couple of events on the AppDomain class, but I'm not sure under which conditions and in which environments they would be fired.
Because this is a low level component, I don't want it to know anything about the environment it's running in, and I don't want a higher level component to need to tell it that it's shutting down.
This component will be used in both a WCF service and a Windows Service application, and I'm also doing tests from a WinForm client app.