2020-03-26 23:10:16 +00:00
|
|
|
using System;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
namespace MediaBrowser.Common.Extensions
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Extension methods for <see cref="Process"/>.
|
|
|
|
/// </summary>
|
|
|
|
public static class ProcessExtensions
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Asynchronously wait for the process to exit.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="process">The process to wait for.</param>
|
2020-03-27 00:09:09 +00:00
|
|
|
/// <param name="timeout">The duration to wait before cancelling waiting for the task.</param>
|
2020-03-26 23:10:16 +00:00
|
|
|
/// <returns>True if the task exited normally, false if the timeout elapsed before the process exited.</returns>
|
2020-04-05 13:25:23 +00:00
|
|
|
/// <exception cref="InvalidOperationException">If <see cref="Process.EnableRaisingEvents"/> is not set to true for the process.</exception>
|
2020-03-27 00:09:09 +00:00
|
|
|
public static async Task<bool> WaitForExitAsync(this Process process, TimeSpan timeout)
|
|
|
|
{
|
|
|
|
using (var cancelTokenSource = new CancellationTokenSource(timeout))
|
|
|
|
{
|
2020-04-04 00:51:30 +00:00
|
|
|
return await WaitForExitAsync(process, cancelTokenSource.Token).ConfigureAwait(false);
|
2020-03-27 00:09:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <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)
|
2020-03-26 23:10:16 +00:00
|
|
|
{
|
|
|
|
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>();
|
2021-08-04 12:40:09 +00:00
|
|
|
process.Exited += (_, _) => tcs.TrySetResult(true);
|
2020-03-26 23:10:16 +00:00
|
|
|
|
|
|
|
// Return immediately if the process has already exited
|
|
|
|
if (process.HasExitedSafe())
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-03-27 00:09:09 +00:00
|
|
|
// Register with the cancellation token then await
|
|
|
|
using (var cancelRegistration = cancelToken.Register(() => tcs.TrySetResult(process.HasExitedSafe())))
|
2020-03-26 23:10:16 +00:00
|
|
|
{
|
|
|
|
return await tcs.Task.ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
}
|
2020-03-27 00:28:24 +00:00
|
|
|
|
|
|
|
/// <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;
|
|
|
|
}
|
|
|
|
}
|
2020-03-26 23:10:16 +00:00
|
|
|
}
|
|
|
|
}
|