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 ?
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());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With