tags:

views:

747

answers:

2

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 ?

+4  A: 

The source parameter will probably be the process that exited. You'll have to cast it.

Roger Lipscombe
+3  A: 

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());
}
lc
exactly what I did -> works like a charm. Thank you.
bernhardrusch