views:

141

answers:

2

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.

A: 

"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."

These two requirements are in conflict. Either the component has to be able to tell when the application is shutting down (in which case it has to know about the environment in which it is running), or some higher level component (which DOES know about the environment) has to tell it to shutdown. There aren't any other options.

In my opinion, components should make as few assumptions as possible about how they are going to be used. For example, they should not assume that they will be used for the entire lifetime of an application, but could be started and stopped at any time, according to the needs of the application. Even if it turns out that the only way the component is ever used is for the lifetime of the application, such an approach leads to a cleaner design. Therefore I would say that the correct approach is for the application to tell the component when it is no longer needed, and it should terminate itself, whether the application is shutting down or not. I would also say that is the typical approach when designing components.

+1  A: 

If you need to handle things at a lower level, maybe you need your component to monitor the application close event. This code is used to monitor a process for closure. (It will also work if someone does an End Task from the task manager.)

Process MyMonitoredProcess = null;
private void Form1_Load(object sender, EventArgs e) 
{
    Process[] p = Process.GetProcessesByName("processName", "machineName");
    if (p.Length > 0)
    {
        MyMonitoredProcess = p[0];
        MyMonitoredProcess.EnableRaisingEvents = true;
        MyMonitoredProcess.Exited += TestProcessEndedEvent;
    } 

}

private void TestProcessEndedEvent(object sender, System.EventArgs e)
{
    // Tasks to be done if application closes.
}

Hope this helps.

Rashmi Pandit