9

I am writing a C# console application and am trying to check when my main program's process has exited, so that I can do some cleanup before exiting, but the event never seems to fire. Here is the code to set the event handler:

Process process = Process.GetCurrentProcess();
CloseConsoleHandler close = new CloseConsoleHandler(test);
process.EnableRaisingEvents = true;
process.Exited += close.CloseHandler; 
//I also tried process.Exited += new EventHandler(close.CloseHandler);

It just never seems to fire, not when the program ends naturally, not when I click the close button... never. Is this even possible?

1
  • 3
    You should try to do your cleanup deterministically, as soon as you're finished with whatever you're cleaning up.
    – SLaks
    Jul 20, 2011 at 14:10

7 Answers 7

9

That won't work; once the process has ended, your code is gone.

Handle the AppDomain.ProcessExit event.

2
  • I tried both. I have now tried implementing a destructor and a dispose method in the appropriate class, but they're never called either. It's so annoying because all I want to do is kill an .exe if it is still open when the program closes.
    – Luke
    Jul 20, 2011 at 14:41
  • 1
    I think that we need to create a second application in order to monitor processes and keep alive the one you want by detecting it has been killed and running it again Mar 5, 2016 at 18:50
7

Process exited event should logically fire AFTER a process exits, so therefore it's impossible to catch it's own exit.

But there are other alternatives, like implementing Dispose in your class to do cleanup, or for some reason you need the Process exited event, just monitor the process from another process.

4
  • I have tried implementing Dispose() and a destructor, but neither is called.
    – Luke
    Jul 20, 2011 at 14:28
  • call Dispose explicitly BEFORE exiting, just for example, if you have a database connection, you'd call the dispose of the settings class that communicates with the database BEFORE you dispose the DB connection. Jul 20, 2011 at 15:06
  • But I want this to be called when the console window is closed by clicking the close button, or when the user presses CTRL+C. I have no idea how to detect when this has happened.
    – Luke
    Jul 20, 2011 at 15:14
  • Maybe a similar question's this answer stackoverflow.com/questions/1119841/… may help.. Jul 20, 2011 at 15:31
3

Use

        Process p = new Process();
        p.EnableRaisingEvents = true;
2

You can add a custom handler to the ProcessExit event and do your cleanup there. For example:

class Program
{
    static void Main(string[] args)
    {
        // Register hanlder to ProcessExit event
        AppDomain.CurrentDomain.ProcessExit += ProcessExitHanlder;
    }

    private static void ProcessExitHanlder(object sender, EventArgs e)
    {
        Console.WriteLine("Cleanup!");
    }
}
1
  • Please, provide a description of the proposed solution, not just some lines of code. May 25, 2016 at 14:57
1

Process.Exit is called only for associated processes and after termination. Try using ProcessExit event of current AppDomain.

1

Or you can try to solve that at application level if you own the main app too (it sounds like): signal the child process from the main app with some technique when the main app is closing. Maybe I'd try to signal synchronously this time, so the main app leaves time for the child for cleanup. This approach has it's own drawbacks though. The ideal would be to catch some system event at your child process.

0

AFAIK there are two ways - depending on your preference one P/Invoke way (see http://osdir.com/ml/windows.devel.dotnet.clr/2003-11/msg00214.html )... this calls a delegate you define in a separate thread upon Exit of your app and works with console...

The other way is AppDomain.ProcessExit - which has a time limit it is allowed to run (default 3 seconds).

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.