4

我正在使用Process.Start()我的 C# 应用程序执行 3 个 exe。我想按顺序运行所有这些 exe。现在,每个都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()仅在第一个完成复制 50 Mb 文件后才开始执行Process.Start()(这大约需要 1 或 2 分钟)。

有什么建议么?

谢谢。

4

3 回答 3

15

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

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 ..

于 2012-03-14T12:44:41.243 回答
0

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

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

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

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();
}
于 2011-06-06T11:31:49.723 回答
0

你可以:

于 2011-06-06T11:23:32.893 回答