From ee2f911a2b85792c2bfc867d42b7d58b847de6ea Mon Sep 17 00:00:00 2001 From: Mark Monteiro Date: Fri, 27 Mar 2020 00:10:16 +0100 Subject: Remove unnecessary CommonProcess abstraction --- .../Extensions/ProcessExtensions.cs | 66 ++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 MediaBrowser.Common/Extensions/ProcessExtensions.cs (limited to 'MediaBrowser.Common/Extensions/ProcessExtensions.cs') diff --git a/MediaBrowser.Common/Extensions/ProcessExtensions.cs b/MediaBrowser.Common/Extensions/ProcessExtensions.cs new file mode 100644 index 000000000..9fa0efdff --- /dev/null +++ b/MediaBrowser.Common/Extensions/ProcessExtensions.cs @@ -0,0 +1,66 @@ +using System; +using System.Diagnostics; +using System.Threading; +using System.Threading.Tasks; + +namespace MediaBrowser.Common.Extensions +{ + /// + /// Extension methods for . + /// + public static class ProcessExtensions + { + /// + /// Gets a value indicating whether the associated process has been terminated using + /// . This is safe to call even if there is no operating system process + /// associated with the . + /// + /// The process to check the exit status for. + /// + /// True if the operating system process referenced by the component has + /// terminated, or if there is no associated operating system process; otherwise, false. + /// + public static bool HasExitedSafe(this Process process) + { + try + { + return process.HasExited; + } + catch (InvalidOperationException) + { + return true; + } + } + + /// + /// Asynchronously wait for the process to exit. + /// + /// The process to wait for. + /// A timeout, in milliseconds, after which to stop waiting for the task. + /// True if the task exited normally, false if the timeout elapsed before the process exited. + public static async Task WaitForExitAsync(this Process process, int timeMs) + { + 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(); + process.Exited += (sender, args) => tcs.TrySetResult(true); + + // Return immediately if the process has already exited + if (process.HasExitedSafe()) + { + 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()))) + { + return await tcs.Task.ConfigureAwait(false); + } + } + } +} -- cgit v1.2.3 From 97c36d11d4e52a8f33d48e382467b6c6068bb6ad Mon Sep 17 00:00:00 2001 From: Mark Monteiro Date: Fri, 27 Mar 2020 01:09:09 +0100 Subject: Use a TimeSpan instead of ms and support providing a custom CancellationToken --- .../Extensions/ProcessExtensions.cs | 23 +++++++++++++++++----- MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs | 4 ++-- .../Subtitles/SubtitleEncoder.cs | 4 ++-- 3 files changed, 22 insertions(+), 9 deletions(-) (limited to 'MediaBrowser.Common/Extensions/ProcessExtensions.cs') 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. /// /// The process to wait for. - /// A timeout, in milliseconds, after which to stop waiting for the task. + /// The duration to wait before cancelling waiting for the task. /// True if the task exited normally, false if the timeout elapsed before the process exited. - public static async Task WaitForExitAsync(this Process process, int timeMs) + public static async Task WaitForExitAsync(this Process process, TimeSpan timeout) + { + using (var cancelTokenSource = new CancellationTokenSource(timeout)) + { + return await WaitForExitAsync(process, cancelTokenSource.Token); + } + } + + /// + /// Asynchronously wait for the process to exit. + /// + /// The process to wait for. + /// A to observe while waiting for the process to exit. + /// True if the task exited normally, false if cancelled before the process exited. + public static async Task 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); } diff --git a/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs b/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs index 62a6c69ca..855b1c754 100644 --- a/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs +++ b/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs @@ -596,7 +596,7 @@ namespace MediaBrowser.MediaEncoding.Encoder timeoutMs = DefaultImageExtractionTimeout; } - ranToCompletion = await process.WaitForExitAsync(timeoutMs).ConfigureAwait(false); + ranToCompletion = await process.WaitForExitAsync(TimeSpan.FromMilliseconds(timeoutMs)).ConfigureAwait(false); if (!ranToCompletion) { @@ -729,7 +729,7 @@ namespace MediaBrowser.MediaEncoding.Encoder while (isResponsive) { - if (await process.WaitForExitAsync(30000).ConfigureAwait(false)) + if (await process.WaitForExitAsync(TimeSpan.FromSeconds(30)).ConfigureAwait(false)) { ranToCompletion = true; break; diff --git a/MediaBrowser.MediaEncoding/Subtitles/SubtitleEncoder.cs b/MediaBrowser.MediaEncoding/Subtitles/SubtitleEncoder.cs index 1736d79cf..f1f0bfeb1 100644 --- a/MediaBrowser.MediaEncoding/Subtitles/SubtitleEncoder.cs +++ b/MediaBrowser.MediaEncoding/Subtitles/SubtitleEncoder.cs @@ -451,7 +451,7 @@ namespace MediaBrowser.MediaEncoding.Subtitles throw; } - var ranToCompletion = await process.WaitForExitAsync(300000).ConfigureAwait(false); + var ranToCompletion = await process.WaitForExitAsync(TimeSpan.FromMinutes(5)).ConfigureAwait(false); if (!ranToCompletion) { @@ -599,7 +599,7 @@ namespace MediaBrowser.MediaEncoding.Subtitles throw; } - var ranToCompletion = await process.WaitForExitAsync(300000).ConfigureAwait(false); + var ranToCompletion = await process.WaitForExitAsync(TimeSpan.FromMinutes(5)).ConfigureAwait(false); if (!ranToCompletion) { -- cgit v1.2.3 From 1c13be085fbc30ac6c5efa893f415fa0d06d557f Mon Sep 17 00:00:00 2001 From: Mark Monteiro Date: Fri, 27 Mar 2020 01:28:24 +0100 Subject: Make HasExitedSafe() private --- .../Extensions/ProcessExtensions.cs | 44 +++++++++++----------- 1 file changed, 22 insertions(+), 22 deletions(-) (limited to 'MediaBrowser.Common/Extensions/ProcessExtensions.cs') diff --git a/MediaBrowser.Common/Extensions/ProcessExtensions.cs b/MediaBrowser.Common/Extensions/ProcessExtensions.cs index 938ced106..525475ba5 100644 --- a/MediaBrowser.Common/Extensions/ProcessExtensions.cs +++ b/MediaBrowser.Common/Extensions/ProcessExtensions.cs @@ -10,28 +10,6 @@ namespace MediaBrowser.Common.Extensions /// public static class ProcessExtensions { - /// - /// Gets a value indicating whether the associated process has been terminated using - /// . This is safe to call even if there is no operating system process - /// associated with the . - /// - /// The process to check the exit status for. - /// - /// True if the operating system process referenced by the component has - /// terminated, or if there is no associated operating system process; otherwise, false. - /// - public static bool HasExitedSafe(this Process process) - { - try - { - return process.HasExited; - } - catch (InvalidOperationException) - { - return true; - } - } - /// /// Asynchronously wait for the process to exit. /// @@ -75,5 +53,27 @@ namespace MediaBrowser.Common.Extensions return await tcs.Task.ConfigureAwait(false); } } + + /// + /// Gets a value indicating whether the associated process has been terminated using + /// . This is safe to call even if there is no operating system process + /// associated with the . + /// + /// The process to check the exit status for. + /// + /// True if the operating system process referenced by the component has + /// terminated, or if there is no associated operating system process; otherwise, false. + /// + private static bool HasExitedSafe(this Process process) + { + try + { + return process.HasExited; + } + catch (InvalidOperationException) + { + return true; + } + } } } -- cgit v1.2.3 From 4efdc63337614762baff4a343c89464cf1b0963f Mon Sep 17 00:00:00 2001 From: Mark Monteiro Date: Fri, 3 Apr 2020 20:51:30 -0400 Subject: Add missing call to ConfigureAwait() --- MediaBrowser.Common/Extensions/ProcessExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'MediaBrowser.Common/Extensions/ProcessExtensions.cs') diff --git a/MediaBrowser.Common/Extensions/ProcessExtensions.cs b/MediaBrowser.Common/Extensions/ProcessExtensions.cs index 525475ba5..6b205049d 100644 --- a/MediaBrowser.Common/Extensions/ProcessExtensions.cs +++ b/MediaBrowser.Common/Extensions/ProcessExtensions.cs @@ -20,7 +20,7 @@ namespace MediaBrowser.Common.Extensions { using (var cancelTokenSource = new CancellationTokenSource(timeout)) { - return await WaitForExitAsync(process, cancelTokenSource.Token); + return await WaitForExitAsync(process, cancelTokenSource.Token).ConfigureAwait(false); } } -- cgit v1.2.3 From 3d8501e462c1b4f9953444873e3ea64e085916a0 Mon Sep 17 00:00:00 2001 From: Mark Monteiro Date: Sun, 5 Apr 2020 09:25:23 -0400 Subject: Document exception --- MediaBrowser.Common/Extensions/ProcessExtensions.cs | 1 + 1 file changed, 1 insertion(+) (limited to 'MediaBrowser.Common/Extensions/ProcessExtensions.cs') diff --git a/MediaBrowser.Common/Extensions/ProcessExtensions.cs b/MediaBrowser.Common/Extensions/ProcessExtensions.cs index 6b205049d..c74787122 100644 --- a/MediaBrowser.Common/Extensions/ProcessExtensions.cs +++ b/MediaBrowser.Common/Extensions/ProcessExtensions.cs @@ -16,6 +16,7 @@ namespace MediaBrowser.Common.Extensions /// The process to wait for. /// The duration to wait before cancelling waiting for the task. /// True if the task exited normally, false if the timeout elapsed before the process exited. + /// If is not set to true for the process. public static async Task WaitForExitAsync(this Process process, TimeSpan timeout) { using (var cancelTokenSource = new CancellationTokenSource(timeout)) -- cgit v1.2.3