How to detect when an application is closed?

I am creating a component in a server application that manages the 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 and then enters a "working" state again.

My problem is that if the process calling the component terminates while that thread is running, the application will not terminate until the thread exits. Basically, my app cannot be disabled if this service is available.

My question is, is there a way to determine if the application is trying to close so that any background threads can exit? I noticed a couple of events in the AppDomain class, but I'm not sure under what conditions and in what environments they will be fired.

Since this is a low-level component, I don't want it to know anything about the environment it is running in, and I don't want the higher-level component to have to say that it is shutting down.

This component will be used in both WCF Service and Windows Service Application, and I am also running tests from a WinForm client application.

0


a source to share


2 answers


If you need to handle things at a lower level, perhaps you need your component to monitor the application close event. This code is used to control the closing process. (It will also work if someone does the end task in 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 it helps.

+1


a source


"Since this is a low-level component, I don't want it to know anything about the environment in which it operates, and I don't want the higher-level component to have to say that it is shutting down."

These two requirements are in conflict. Either the component must be able to tell when the application is closing (in which case it must be aware of the environment it is running in), or some higher-level component (which knows about the environment) must tell it to shut down. There are no other options.



In my opinion, components should make as few assumptions as possible about how they will be used. For example, they should not assume that they will be used for the entire life of the application, but can be started and stopped at any time according to the needs of the application. Even if it turns out that the only way to use a component is through the lifetime of the application, this approach leads to cleaner designs. So I would say the correct approach is for the application to tell the component when it is no longer needed and it should stop, whether the application exits or not. I would also say that this is the typical approach when designing components.

0


a source







All Articles