Use a TimeSpan instead of ms and support providing a custom CancellationToken

This commit is contained in:
Mark Monteiro 2020-03-27 01:09:09 +01:00
parent 7447ea8960
commit 97c36d11d4
3 changed files with 22 additions and 9 deletions

View File

@ -36,9 +36,23 @@ namespace MediaBrowser.Common.Extensions
/// Asynchronously wait for the process to exit. /// Asynchronously wait for the process to exit.
/// </summary> /// </summary>
/// <param name="process">The process to wait for.</param> /// <param name="process">The process to wait for.</param>
/// <param name="timeMs">A timeout, in milliseconds, after which to stop waiting for the task.</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> /// <returns>True if the task exited normally, false if the timeout elapsed before the process exited.</returns>
public static async Task<bool> WaitForExitAsync(this Process process, int timeMs) public static async Task<bool> WaitForExitAsync(this Process process, TimeSpan timeout)
{
using (var cancelTokenSource = new CancellationTokenSource(timeout))
{
return await WaitForExitAsync(process, cancelTokenSource.Token);
}
}
/// <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) if (!process.EnableRaisingEvents)
{ {
@ -55,9 +69,8 @@ namespace MediaBrowser.Common.Extensions
return true; return true;
} }
// Add an additional timeout then await // Register with the cancellation token then await
using (var cancelTokenSource = new CancellationTokenSource(Math.Max(0, timeMs))) using (var cancelRegistration = cancelToken.Register(() => tcs.TrySetResult(process.HasExitedSafe())))
using (var cancelRegistration = cancelTokenSource.Token.Register(() => tcs.TrySetResult(process.HasExitedSafe())))
{ {
return await tcs.Task.ConfigureAwait(false); return await tcs.Task.ConfigureAwait(false);
} }

View File

@ -596,7 +596,7 @@ namespace MediaBrowser.MediaEncoding.Encoder
timeoutMs = DefaultImageExtractionTimeout; timeoutMs = DefaultImageExtractionTimeout;
} }
ranToCompletion = await process.WaitForExitAsync(timeoutMs).ConfigureAwait(false); ranToCompletion = await process.WaitForExitAsync(TimeSpan.FromMilliseconds(timeoutMs)).ConfigureAwait(false);
if (!ranToCompletion) if (!ranToCompletion)
{ {
@ -729,7 +729,7 @@ namespace MediaBrowser.MediaEncoding.Encoder
while (isResponsive) while (isResponsive)
{ {
if (await process.WaitForExitAsync(30000).ConfigureAwait(false)) if (await process.WaitForExitAsync(TimeSpan.FromSeconds(30)).ConfigureAwait(false))
{ {
ranToCompletion = true; ranToCompletion = true;
break; break;

View File

@ -451,7 +451,7 @@ namespace MediaBrowser.MediaEncoding.Subtitles
throw; throw;
} }
var ranToCompletion = await process.WaitForExitAsync(300000).ConfigureAwait(false); var ranToCompletion = await process.WaitForExitAsync(TimeSpan.FromMinutes(5)).ConfigureAwait(false);
if (!ranToCompletion) if (!ranToCompletion)
{ {
@ -599,7 +599,7 @@ namespace MediaBrowser.MediaEncoding.Subtitles
throw; throw;
} }
var ranToCompletion = await process.WaitForExitAsync(300000).ConfigureAwait(false); var ranToCompletion = await process.WaitForExitAsync(TimeSpan.FromMinutes(5)).ConfigureAwait(false);
if (!ranToCompletion) if (!ranToCompletion)
{ {