Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding out which System.Diagnostics.Process has finished

Tags:

c#

.net

process

I am spawning new processes in my C# application with System.Diagnostics.Process like this:

void SpawnNewProcess
{
    string fileName = GetFileName();
    System.Diagnostics.Process proc = new System.Diagnostics.Process();
    proc.StartInfo.FileName = fileName;
    proc.Start();
    proc.Exited += new EventHandler(ProcessExited);
    proc.EnableRaisingEvents = true;
}          

private void ProcessExited(Object source, EventArgs e)
{ 

}

The user is free to spawn as many processes as he likes - now the question: I'm in the ProcessExited function, how do I find out which of the processes has quit ?

The example in the MSDN just shows how to use a member variable for this - but this wouldn't work with more processes.

Any ideas how I find out which process just exited ?

like image 311
bernhardrusch Avatar asked Dec 29 '25 12:12

bernhardrusch


1 Answers

You will get the Process object as source in your event handler. source.Id will have the PID of the process. If you need more information, you can keep a lookup table of PIDs and associated properties as a member variable.

Note that you'll have to cast source to a Process before being able to access its members. For example:

private void ProcessExited(Object source, EventArgs e)
{ 
    var proc = (Process)source;
    Console.WriteLine(proc.Id.ToString());
}
like image 78
lc. Avatar answered Dec 31 '25 01:12

lc.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!