From 48facb797ed912e4ea6b04b17d1ff190ac2daac4 Mon Sep 17 00:00:00 2001 From: stefan Date: Wed, 12 Sep 2018 19:26:21 +0200 Subject: Update to 3.5.2 and .net core 2.1 --- .../Diagnostics/CommonProcess.cs | 49 +++++++++++++++++++++- 1 file changed, 47 insertions(+), 2 deletions(-) (limited to 'Emby.Server.Implementations/Diagnostics/CommonProcess.cs') diff --git a/Emby.Server.Implementations/Diagnostics/CommonProcess.cs b/Emby.Server.Implementations/Diagnostics/CommonProcess.cs index a0a5f32ef6..a709607bde 100644 --- a/Emby.Server.Implementations/Diagnostics/CommonProcess.cs +++ b/Emby.Server.Implementations/Diagnostics/CommonProcess.cs @@ -3,6 +3,7 @@ using System.Diagnostics; using System.IO; using System.Threading.Tasks; using MediaBrowser.Model.Diagnostics; +using System.Threading; namespace Emby.Server.Implementations.Diagnostics { @@ -48,8 +49,32 @@ namespace Emby.Server.Implementations.Diagnostics } } + private bool _hasExited; + private bool HasExited + { + get + { + if (_hasExited) + { + return true; + } + + try + { + _hasExited = _process.HasExited; + } + catch (InvalidOperationException) + { + _hasExited = true; + } + + return _hasExited; + } + } + private void _process_Exited(object sender, EventArgs e) { + _hasExited = true; if (Exited != null) { Exited(this, e); @@ -98,13 +123,33 @@ namespace Emby.Server.Implementations.Diagnostics public Task WaitForExitAsync(int timeMs) { - return Task.FromResult(_process.WaitForExit(timeMs)); + //if (_process.WaitForExit(100)) + //{ + // return Task.FromResult(true); + //} + + //timeMs -= 100; + timeMs = Math.Max(0, timeMs); + + var tcs = new TaskCompletionSource(); + + var cancellationToken = new CancellationTokenSource(timeMs).Token; + + if (HasExited) + { + return Task.FromResult(true); + } + + _process.Exited += (sender, args) => tcs.TrySetResult(true); + + cancellationToken.Register(() => tcs.TrySetResult(HasExited)); + + return tcs.Task; } public void Dispose() { _process.Dispose(); - GC.SuppressFinalize(this); } } } -- cgit v1.2.3