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>
|
2023-10-09 22:18:50 +00:00
|
|
|
/// <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)
|
2020-03-27 00:09:09 +00:00
|
|
|
{
|
|
|
|
using (var cancelTokenSource = new CancellationTokenSource(timeout))
|
|
|
|
{
|
2023-10-09 22:18:50 +00:00
|
|
|
await process.WaitForExitAsync(cancelTokenSource.Token).ConfigureAwait(false);
|
2020-03-27 00:28:24 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-26 23:10:16 +00:00
|
|
|
}
|
|
|
|
}
|