Update async process handling

This commit is contained in:
Fergal Moran
2018-03-06 18:54:26 +00:00
parent 1fb54b9c10
commit 1648794283
4 changed files with 46 additions and 11 deletions

View File

@@ -0,0 +1,25 @@
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;
}
}
}