Use Process.WaitForExitAsync from .NET 5.
MODIFY
WalletWasabi/Microservices/ProcessAsync.cs
WalletWasabi/Microservices/ProcessAsync.cs
@@ -25,15 +25,9 @@ namespace WalletWasabi.Microservices
private ProcessAsync(Process process)
{
- ProcessExecutionTcs = new TaskCompletionSource<bool>();
-
Process = process;
- Process.EnableRaisingEvents = true;
- Process.Exited += OnExited;
}
- private TaskCompletionSource<bool> ProcessExecutionTcs { get; }
-
private Process Process { get; }
/// <inheritdoc cref="Process.StartInfo"/>
@@ -90,10 +84,10 @@ namespace WalletWasabi.Microservices
/// <summary>
/// Waits until the process either finishes on its own or when user cancels the action.
/// </summary>
- /// <param name="cancel">Cancellation token.</param>
- /// <param name="killOnCancel">If <c>true</c> the process will be killed (with entire process tree) when this asynchronous action is canceled via <paramref name="cancel"/> token.</param>
+ /// <param name="cancellationToken">Cancellation token.</param>
+ /// <param name="killOnCancel">If <c>true</c> the process will be killed (with entire process tree) when this asynchronous action is canceled via <paramref name="cancellationToken"/> token.</param>
/// <returns><see cref="Task"/>.</returns>
- public async Task WaitForExitAsync(CancellationToken cancel, bool killOnCancel = false)
+ public async Task WaitForExitAsync(CancellationToken cancellationToken, bool killOnCancel = false)
{
if (Process.HasExited)
{
@@ -102,17 +96,13 @@ namespace WalletWasabi.Microservices
}
try
- {
- // If this token is already in the canceled state, the delegate will be run immediately and synchronously.
- using (cancel.Register(() => ProcessExecutionTcs.TrySetCanceled()))
{
Logger.LogTrace("Wait for the process to exit.");
- await ProcessExecutionTcs.Task.ConfigureAwait(false);
+ await Process.WaitForExitAsync(cancellationToken).ConfigureAwait(false);
Logger.LogTrace("Process has exited.");
}
- }
catch (OperationCanceledException ex)
{
Logger.LogTrace("User canceled waiting for process exit.");
@@ -133,7 +123,7 @@ namespace WalletWasabi.Microservices
}
}
- throw new TaskCanceledException("Waiting for process exiting was canceled.", ex, cancel);
+ throw new TaskCanceledException("Waiting for process exiting was canceled.", ex, cancellationToken);
}
}
@@ -148,7 +138,6 @@ namespace WalletWasabi.Microservices
if (disposing)
{
// Dispose managed state (managed objects).
- Process.Exited -= OnExited;
Process.Dispose();
}
@@ -163,10 +152,5 @@ namespace WalletWasabi.Microservices
// Suppress finalization.
GC.SuppressFinalize(this);
}
-
- private void OnExited(object? sender, EventArgs? e)
- {
- ProcessExecutionTcs.TrySetResult(true);
- }
}
}

Use Process.WaitForExitAsync from .NET 5.