mirror of
https://github.com/fergalmoran/podnoms.git
synced 2026-01-02 22:56:45 +00:00
Update async process handling
This commit is contained in:
25
server/Utils/Extensions/ProcessExtensions.cs
Normal file
25
server/Utils/Extensions/ProcessExtensions.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user