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

时间:2014-05-19 15:08:30

标签: c# command-line process wait

我需要执行许多命令行脚本。它们目前存储在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

3 个答案:

答案 0 :(得分:7)

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

var processes = new List<Process>();
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()在评论中添加了

答案 1 :(得分:5)

使用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);

答案 2 :(得分:0)

至少在Windows上,您可以使用https://flutter.io/flutter-for-android/#how-do-i-handle-incoming-intents-from-external-applications-in-flutter

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重载,例如等待超时。