4

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!

skaffman
  • 390,936
  • 96
  • 800
  • 764
Inferis
  • 4,452
  • 4
  • 36
  • 47

2 Answers2

4

Have a look here: atexit, exit delegate in c#

Community
  • 1
  • 1
Vlad
  • 34,303
  • 6
  • 78
  • 192
0

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
  • 20,544
  • 16
  • 68
  • 92
  • 2
    I'm not starting a process. I need to know when the current process is about to go the way of the dodo. – Inferis Mar 01 '10 at 14:06