diff options
Diffstat (limited to 'MediaBrowser.Common/Extensions')
| -rw-r--r-- | MediaBrowser.Common/Extensions/ProcessExtensions.cs | 23 |
1 files changed, 18 insertions, 5 deletions
diff --git a/MediaBrowser.Common/Extensions/ProcessExtensions.cs b/MediaBrowser.Common/Extensions/ProcessExtensions.cs index 9fa0efdff..938ced106 100644 --- a/MediaBrowser.Common/Extensions/ProcessExtensions.cs +++ b/MediaBrowser.Common/Extensions/ProcessExtensions.cs @@ -36,9 +36,23 @@ namespace MediaBrowser.Common.Extensions /// Asynchronously wait for the process to exit. /// </summary> /// <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> - 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) { @@ -55,9 +69,8 @@ namespace MediaBrowser.Common.Extensions return true; } - // Add an additional timeout then await - using (var cancelTokenSource = new CancellationTokenSource(Math.Max(0, timeMs))) - using (var cancelRegistration = cancelTokenSource.Token.Register(() => tcs.TrySetResult(process.HasExitedSafe()))) + // Register with the cancellation token then await + using (var cancelRegistration = cancelToken.Register(() => tcs.TrySetResult(process.HasExitedSafe()))) { return await tcs.Task.ConfigureAwait(false); } |
