等到多个命令行进程完成?

我需要执行许多命令行脚本。 它们目前存储在List 。 我想同时运行它们,并且只有在所有这些步骤完成后才继续执行下一步。

我已经尝试了下面显示的方法,但发现它缺乏,因为最后一个命令不一定最后结束 。 事实上,我发现最后一个命令甚至可以是第一个完成的命令。 所以,我相信我需要像WaitForExit()这样的东西,但是在所有正在执行的进程完成之前它不会返回。

 for (int i = 0; i < commands.Count; i++) { string strCmdText = commands[i]; var process = System.Diagnostics.Process.Start("CMD.exe", strCmdText); if (i == (commands.Count - 1)) { process.WaitForExit(); } } //next course of action once all the above is done 

由于每次调用Process.Start启动一个新进程,您可以单独跟踪它们,如下所示:

 var processes = new List(); for (int i = 0; i < commands.Count; i++) { string strCmdText = commands[i]; processes.Add(System.Diagnostics.Process.Start("CMD.exe", strCmdText)); } foreach(var process in processes) { process.WaitForExit(); process.Close(); } 

编辑

在注释中添加了Process.Close()

使用Task数组并等待所有这些完成。

 var tasks = new Task[commands.Count]; for (int i = 0; i < commands.Count; i++) { tasks[i] = Task.Factory.StartNew(() => { string strCmdText = commands[i]; var process = System.Diagnostics.Process.Start("CMD.exe", strCmdText); process.WaitForExit(); }); } Task.WaitAll(tasks); 

或者,更多LINQ – 就像这样:

 var tasks = commands.Select(strCmdText => Task.Factory.StartNew(() => { var process = System.Diagnostics.Process.Start("CMD.exe", strCmdText); process.WaitForExit(); })).ToArray(); Task.WaitAll(tasks); 

至少在Windows上,您可以使用WaitHandle.WaitAll()

 using System; using System.Diagnostics; using System.Threading; using Microsoft.Win32.SafeHandles; using static System.FormattableString; public class ProcessWaitHandle : WaitHandle { public ProcessWaitHandle(Process process) => this.SafeWaitHandle = new SafeWaitHandle(process.Handle, false); } class Program { static void Main(string[] args) { int processesCount = 42; var processes = new Process[processesCount]; var waitHandles = new WaitHandle[processesCount]; try { for (int i = 0; processesCount > i; ++i) { // exit immediately with return code i Process process = Process.Start( "cmd.exe", Invariant($"/C \"exit {i}\"")); processes[i] = process; waitHandles[i] = new ProcessWaitHandle(process); } WaitHandle.WaitAll(waitHandles); foreach (Process p in processes) { Console.Error.WriteLine( Invariant($"process with Id {p.Id} exited with code {p.ExitCode}")); } } finally { foreach (Process p in processes) { p?.Dispose(); } foreach (WaitHandle h in waitHandles) { h?.Dispose(); } } Console.WriteLine("Press any key to continue..."); Console.ReadKey(false); } } 

例如,这种方法还可以使用其他WaitAll重载,并等待超时。