2011-06-06 23 views
3

我在C#应用程序中使用Process.Start()执行3个exes。我想依次运行所有这些exes。现在,每个Process.Start()都以并行方式自行执行。在C#中执行多个Process.Start()#

如:

Process.Start(exe1ForCopying_A_50_Mb_File);  
Process.Start(exe2ForCopying_A_10_Mb_File); 
Process.Start(exe3ForCopying_A_20_Mb_File); 

我想我的第二Process.Start()开始执行后,才第一次Process.Start()完成复制50 MB的文件(这将采取约1或2分钟)。

有什么建议吗?

谢谢。

回答

8

我想我自己得到了答案..! :)

Process process = new Process(); 
ProcessStartInfo startInfo = new ProcessStartInfo(); 
startInfo.FileName = MyExe; 
startInfo.Arguments = ArgumentsForMyExe; 
process.StartInfo = startInfo; 
process.Start(); 
process.WaitForExit(); // This is the line which answers my question :) 

感谢您的建议VAShhh ..

0

您可以:

+1

我觉得我得到了自己的答案..! :) Process process = new Process(); ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = MyExe; startInfo.Arguments = ArgumentsForMyExe; process.StartInfo = startInfo; process.Start(); process.WaitForExit(); //这是回答我的问题的线:) 感谢您的建议VAShhh .. – Sandeep 2011-06-06 11:26:25

+0

如果我已经很好地理解了这个问题,那么您编写的代码会使并行进程结束时的程序“阻塞”或“等待” 。你确定这不会导致界面冻结吗?使用事件不会这样做 – VAShhh 2011-06-06 11:30:46

0

您可以启动后台线程或任务并在循环中同步等待(使用WaitForExit),也可以使用异步方法。

逐个创建Process对象,并将事件处理程序挂接到继续执行下一个Process的Exited事件。使用Process构造函数创建它们,挂钩Exited事件处理程序,然后调用Start;否则,使用静态Process.Start,如果进程在Process.Start返回和附加事件处理程序之间失败,我认为事件处理程序不会被调用,因为它严格已经退出。

证明的概念:(不处理处置,队列访问不是线程安全的,尽管它应该足够了,如果它确实是串行的,等等)

Queue<Process> ProcessesToRun = new Queue<Process>(new []{ new Process("1"), new Process("2"), new Process("3") }); 

void ProcessExited(object sender, System.EventArgs e) { 
    GrabNextProcessAndRun(); 
} 

void GrabNextProcessAndRun() { 
    if (ProcessesToRun.Count > 0) { 
     Process process = ProcessesToRun.Dequeue(); 
     process.Exited += ProcessExited; 
     process.Start(); 
    } 
} 

void TheEntryPoint() { 
    GrabNextProcessAndRun(); 
} 
相关问题