Files
podnoms/server/Utils/Extensions/ProcessExtensions.cs
2018-04-13 22:03:05 +01:00

25 lines
1.1 KiB
C#

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