using System.Diagnostics; using System.Threading; using System.Threading.Tasks; namespace PodNoms.Api.Utils.Extensions { public static class ProcessExtensions { /// /// Waits asynchronously for the process to exit. /// /// The process to wait for cancellation. /// A cancellation token. If invoked, the task will return /// immediately as canceled. /// A Task representing waiting for the process to end. public static Task WaitForExitAsync (this Process process, CancellationToken cancellationToken = default (CancellationToken)) { var tcs = new TaskCompletionSource (); process.EnableRaisingEvents = true; process.Exited += (sender, args) => tcs.TrySetResult (null); if (cancellationToken != default (CancellationToken)) cancellationToken.Register (tcs.SetCanceled); return tcs.Task; } } }