【发布时间】:2018-04-11 03:56:52
【问题描述】:
我想同时调用三个相同的 EXE,当它们都终止时,我期望三个返回值,这是我到目前为止的结果:
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = MyEXEPath;
for(int i=0;i<3;i++)
{
p.StartInfo.Arguments = para1[i] + " " + para2[i];
p.Start();
Console.WriteLine("Start run");
}
p.WaitForExit();
int result = p.ExitCode;
Console.WriteLine("Return info:" + results); //Problem: I can only get the last return value
你可以看到我只有一个返回值,而不是三个,所以我想知道我是否可以这样做:
int[] results = new int[3];
System.Diagnostics.Process p[] = new System.Diagnostics.Process()[3];
for(int i=0;i<3;i++)
{
p[i].StartInfo.FileName = MyEXEPath;
p[i].StartInfo.Arguments = para1[i] + " " + para2[i];
p[i].Start();
Console.WriteLine("Start run");
p[i].EnableRaisingEvents = true;
p[i].Exited += (sender, e) =>
{
results[i] = p[i].ExitCode;
Console.WriteLine("Return info:" + results[i]);
};
}
while(results[0] != 0 && results[1] != 0 && results[2] != 0 )
{
break; //all EXEs ternimated, break and continue my job
}
确定编译失败,System.Diagnostics.Process p[] = new System.Diagnostics.Process()[3];
那么我该如何解决它或有其他方法?
【问题讨论】:
-
var p = new [] { new Process(), new Process(), new Process() };
标签: c# exit-code system.diagnostics