There are several objects of type System.Diagnostic.Process . It is required to ensure waiting for the completion of these processes using WaitForExit(int milliseconds) , but so that the total time does not exceed in times milliseconds . Those. in fact, this action must be parallelized.

What is the canonical way to do this in C # ? First of all, the variant that is available for implementation in VS2013 ( .Net 4.0 ) is of interest. As a supplement, you can bring the most modern version, if it is different.

    2 answers 2

    You can use the Exited event and the good old ManualResetEvent / ManualResetEventSlim :

     class Program { static void Main(string[] args) { var reset1 = new ManualResetEventSlim(); var reset2 = new ManualResetEventSlim(); var p1 = new Process(); p1.StartInfo = new ProcessStartInfo(@"c:\windows\system32\notepad.exe"); p1.EnableRaisingEvents = true; p1.Exited += (s, e) => reset1.Set(); p1.Start(); var p2 = new Process(); p2.StartInfo = new ProcessStartInfo(@"c:\windows\system32\notepad.exe"); p2.EnableRaisingEvents = true; p2.Exited += (s, e) => reset2.Set(); p2.Start(); var onTime = WaitHandle.WaitAll(new[] { reset1.WaitHandle, reset2.WaitHandle }, 10000); Console.WriteLine($"Completed on time: {onTime}"); } } 

    Also now in the trend to expect using the modern API: wrapping events in TaskCompletionSource (although, of course, sometimes this is too sophisticated). You can use the Exited event, TaskCompletionSource and the Task.WaitAny() method:

     class Program { static void Main(string[] args) { var tcs1 = new TaskCompletionSource<byte>(); var tcs2 = new TaskCompletionSource<byte>(); var p1 = new Process(); p1.StartInfo = new ProcessStartInfo(@"c:\windows\system32\notepad.exe"); p1.EnableRaisingEvents = true; p1.Exited += (s, e) => tcs1.SetResult(0); p1.Start(); var p2 = new Process(); p2.StartInfo = new ProcessStartInfo(@"c:\windows\system32\notepad.exe"); p2.EnableRaisingEvents = true; p2.Exited += (s, e) => tcs2.SetResult(0); p2.Start(); var completedTaskIndex = Task.WaitAny(Task.Delay(10000), tcs1.Task, tcs2.Task); Console.WriteLine($"Completed on time: {completedTaskIndex > 0}"); } } 

      Try WaitHandle.WaitAll, and as a list of handles, collect Process.Handle of all processes.

      • And nothing, that WaitAll expects the type WaitHandle[] , and Process.Handle has the type IntPtr ? - αλεχολυτ
      • It's easy - stackoverflow.com/questions/26852305/… - Alexander Alexeev
      • You can write your own successor, WaitHandle, or you can use ManualResetEvent - Alexander Alexeev