我试图并行运行两个exe程序。我想启动它们,等待它们完成然后再继续。这是一个例子:
static void Main(string[] args)
{
ProcessStartInfo proc1Info = new ProcessStartInfo("Testexe.exe");
proc1Info.ErrorDialog = false;
proc1Info.UseShellExecute = false;
proc1Info.RedirectStandardOutput = true;
proc1Info.RedirectStandardError = true;
proc1Info.Arguments = "5000";
ProcessStartInfo proc2Info = new ProcessStartInfo("Testexe1.exe");
proc2Info.ErrorDialog = false;
proc2Info.UseShellExecute = false;
proc2Info.RedirectStandardOutput = true;
proc2Info.RedirectStandardError = true;
proc2Info.Arguments = "10000";
Console.WriteLine("Start Proc 1");
Process proc1 = new Process();
proc1.StartInfo = proc1Info;
var task1 = Task.Factory.StartNew(() => proc1.Start());
Console.WriteLine("Start Proc 2");
Process proc2 = new Process();
proc2.StartInfo = proc2Info;
var task2 = Task.Factory.StartNew(() => proc2.Start());
Console.WriteLine("Wait for tasks");
Task.WaitAll(task1, task2);
Console.WriteLine("Procs complete");
}
第一个进程将睡5秒,第二个进程进入10秒。我希望看到procs已经开始的两条消息和消息"等待任务"。然后10秒后看到" Procs complete"。但是,我看到" Procs完成"立即。
任务管理器显示在"过程完成时运行的过程"显示。
那么,如何从单个程序启动两个单独的可执行程序,并行运行它们然后等待它们继续之前完成?
答案 0 :(得分:0)
找到解决方案: static void Main(string [] args) { ProcessStartInfo proc1Info = new ProcessStartInfo(“Testexe.exe”); proc1Info.ErrorDialog = false; proc1Info.UseShellExecute = false; proc1Info.RedirectStandardOutput = true; proc1Info.RedirectStandardError = true; proc1Info.Arguments =“5000”;
ProcessStartInfo proc2Info = new ProcessStartInfo("Testexe1.exe");
proc2Info.ErrorDialog = false;
proc2Info.UseShellExecute = false;
proc2Info.RedirectStandardOutput = true;
proc2Info.RedirectStandardError = true;
proc2Info.Arguments = "10000";
Console.WriteLine("Start Proc 1");
Process proc1 = new Process();
proc1.StartInfo = proc1Info;
proc1.Start();
Console.WriteLine("Start Proc 2");
Process proc2 = new Process();
proc2.StartInfo = proc2Info;
proc2.Start();
Console.WriteLine("Wait for Procs");
Stopwatch sw = new Stopwatch();
sw.Start();
proc1.WaitForExit();
proc2.WaitForExit();
sw.Stop();
Console.WriteLine("Procs complete in '{0}' ms", sw.ElapsedMilliseconds);
}