mirror of
https://github.com/fergalmoran/podnoms.git
synced 2025-12-22 17:28:43 +00:00
25 lines
1.1 KiB
C#
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;
|
|
}
|
|
}
|
|
} |