I am spawning new processes in my C# application with System.Diagnostics.Process like this:
我使用System.Diagnostics.Process在我的C#应用程序中生成新进程,如下所示:
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 ?
用户可以随心所欲地生成尽可能多的进程 - 现在的问题是:我在ProcessExited函数中,如何找出已退出的进程?
The example in the MSDN just shows how to use a member variable for this - but this wouldn't work with more processes.
MSDN中的示例仅显示如何使用成员变量 - 但这不适用于更多进程。
Any ideas how I find out which process just exited ?
任何想法我如何找出刚刚退出的流程?
2 个解决方案
#1
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.
您将在事件处理程序中将Process对象作为源。 source.Id将拥有该进程的PID。如果需要更多信息,可以将PID和关联属性的查找表保留为成员变量。
Note that you'll have to cast source
to a Process
before being able to access its members. For example:
请注意,在能够访问其成员之前,您必须将源代码转换为Process。例如:
private void ProcessExited(Object source, EventArgs e)
{
var proc = (Process)source;
Console.WriteLine(proc.Id.ToString());
}
#2
The source
parameter will probably be the process that exited. You'll have to cast it.
source参数可能是退出的进程。你必须施展它。
#1
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.
您将在事件处理程序中将Process对象作为源。 source.Id将拥有该进程的PID。如果需要更多信息,可以将PID和关联属性的查找表保留为成员变量。
Note that you'll have to cast source
to a Process
before being able to access its members. For example:
请注意,在能够访问其成员之前,您必须将源代码转换为Process。例如:
private void ProcessExited(Object source, EventArgs e)
{
var proc = (Process)source;
Console.WriteLine(proc.Id.ToString());
}
#2
The source
parameter will probably be the process that exited. You'll have to cast it.
source参数可能是退出的进程。你必须施展它。