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
  • 398,947
  • 96
  • 818
  • 769
Inferis
  • 4,582
  • 5
  • 37
  • 47

2 Answers2

4

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

Community
  • 1
  • 1
Vlad
  • 35,022
  • 6
  • 77
  • 199
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
  • 21,468
  • 17
  • 69
  • 94
  • 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