tags:

views:

63

answers:

2

Is there a way to run a bit of code when the current process is getting terminated?

I want to log some stuff when a process terminates (either through external means - eg killing it - or quitting in the application itself).

We're talking about a Console application written in c#.

Thanks!

+1  A: 

I am not sure, but something similar would help

Process process = new Process();
.
.
process.Exited += new EventHandler(myProcess_Exited);
process.Start();

private void myProcess_Exited(object sender, System.EventArgs e)
{    
  eventHandled = true;
  customAction(); // your logging stuff here
}
public void customAction()
{
  //
}

have a look at: Process.Exited Event

Asad Butt
I'm not starting a process. I need to know when the current process is about to go the way of the dodo.
Inferis
how about `Process currentProcess = Process.GetCurrentProcess();` `currentProcess.Exited += new EventHandler(myProcess_Exited); `
Asad Butt
Tried that. No go.
Inferis
+2  A: 

Have a look here: http://stackoverflow.com/questions/1679876/atexit-exit-delegate-in-c

Vlad