Inferis
Inferis

Reputation: 4662

Run code when current process terminates?

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!

Upvotes: 5

Views: 2732

Answers (2)

Vlad
Vlad

Reputation: 35594

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

Upvotes: 4

Asad
Asad

Reputation: 21938

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

Upvotes: 0

Related Questions