I have a need to execute many command line scripts. They are currently stored in a List
. I want to run them all at the same time and proceed with the next step only after all of them have completed.
I have tried the approach that I show below, but found it lacking because the last command doesn't necessarily end last. In fact, I found that the last command can even be the first to complete. So, I believe that I need something like WaitForExit()
, but which doesn't return until all executing processes have completed.
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
Since each call to Process.Start
starts a new process, you can track them all separately, like so:
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();
}
EDIT
Process.Close()
added as in the comments
Use a Task array and wait for all of them to complete.
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);
Or, more LINQ-ishly like this:
var tasks = commands.Select(strCmdText => Task.Factory.StartNew(() => {
var process = System.Diagnostics.Process.Start("CMD.exe", strCmdText);
process.WaitForExit();
})).ToArray();
Task.WaitAll(tasks);
At least on Windows, you could use 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);
}
}
This approach offers the possibility to also use other WaitAll
overloads, and wait with timeouts, for example.