aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Common
diff options
context:
space:
mode:
authorClaus Vium <cvium@users.noreply.github.com>2023-10-10 20:12:07 +0200
committerGitHub <noreply@github.com>2023-10-10 20:12:07 +0200
commit733ee12ee47e2f744af5f605d82d689088b3fe9b (patch)
tree12ec3e4cbe6ba3e5db64cb8e558ff52c8dd4b4aa /MediaBrowser.Common
parentba23c880f3ac5e5892a730b20ab78d8bc78d91bb (diff)
parent4757ce105bedb075c6663d92bc1e93f87c4779f2 (diff)
Merge pull request #10378 from Bond-009/waitforexitasync
Use Process.WaitForExitAsync added in .NET 5
Diffstat (limited to 'MediaBrowser.Common')
-rw-r--r--MediaBrowser.Common/Extensions/ProcessExtensions.cs60
1 files changed, 4 insertions, 56 deletions
diff --git a/MediaBrowser.Common/Extensions/ProcessExtensions.cs b/MediaBrowser.Common/Extensions/ProcessExtensions.cs
index c3a7cb394..bb8ab130d 100644
--- a/MediaBrowser.Common/Extensions/ProcessExtensions.cs
+++ b/MediaBrowser.Common/Extensions/ProcessExtensions.cs
@@ -15,65 +15,13 @@ namespace MediaBrowser.Common.Extensions
/// </summary>
/// <param name="process">The process to wait for.</param>
/// <param name="timeout">The duration to wait before cancelling waiting for the task.</param>
- /// <returns>True if the task exited normally, false if the timeout elapsed before the process exited.</returns>
- /// <exception cref="InvalidOperationException">If <see cref="Process.EnableRaisingEvents"/> is not set to true for the process.</exception>
- public static async Task<bool> WaitForExitAsync(this Process process, TimeSpan timeout)
+ /// <returns>A task that will complete when the process has exited, cancellation has been requested, or an error occurs.</returns>
+ /// <exception cref="OperationCanceledException">The timeout ended.</exception>
+ public static async Task WaitForExitAsync(this Process process, TimeSpan timeout)
{
using (var cancelTokenSource = new CancellationTokenSource(timeout))
{
- return await WaitForExitAsync(process, cancelTokenSource.Token).ConfigureAwait(false);
- }
- }
-
- /// <summary>
- /// Asynchronously wait for the process to exit.
- /// </summary>
- /// <param name="process">The process to wait for.</param>
- /// <param name="cancelToken">A <see cref="CancellationToken"/> to observe while waiting for the process to exit.</param>
- /// <returns>True if the task exited normally, false if cancelled before the process exited.</returns>
- public static async Task<bool> WaitForExitAsync(this Process process, CancellationToken cancelToken)
- {
- if (!process.EnableRaisingEvents)
- {
- throw new InvalidOperationException("EnableRisingEvents must be enabled to async wait for a task to exit.");
- }
-
- // Add an event handler for the process exit event
- var tcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
- process.Exited += (_, _) => tcs.TrySetResult(true);
-
- // Return immediately if the process has already exited
- if (process.HasExitedSafe())
- {
- return true;
- }
-
- // Register with the cancellation token then await
- using (var cancelRegistration = cancelToken.Register(() => tcs.TrySetResult(process.HasExitedSafe())))
- {
- return await tcs.Task.ConfigureAwait(false);
- }
- }
-
- /// <summary>
- /// Gets a value indicating whether the associated process has been terminated using
- /// <see cref="Process.HasExited"/>. This is safe to call even if there is no operating system process
- /// associated with the <see cref="Process"/>.
- /// </summary>
- /// <param name="process">The process to check the exit status for.</param>
- /// <returns>
- /// True if the operating system process referenced by the <see cref="Process"/> component has
- /// terminated, or if there is no associated operating system process; otherwise, false.
- /// </returns>
- private static bool HasExitedSafe(this Process process)
- {
- try
- {
- return process.HasExited;
- }
- catch (InvalidOperationException)
- {
- return true;
+ await process.WaitForExitAsync(cancelTokenSource.Token).ConfigureAwait(false);
}
}
}