jellyfin-server/Emby.Server.Implementations/Diagnostics/CommonProcess.cs

141 lines
3.5 KiB
C#
Raw Normal View History

2016-11-01 04:07:12 +00:00
using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using MediaBrowser.Model.Diagnostics;
2018-09-12 17:26:21 +00:00
using System.Threading;
2016-11-01 04:07:12 +00:00
namespace Emby.Server.Implementations.Diagnostics
2016-11-01 04:07:12 +00:00
{
public class CommonProcess : IProcess
{
public event EventHandler Exited;
private readonly ProcessOptions _options;
private readonly Process _process;
public CommonProcess(ProcessOptions options)
{
_options = options;
var startInfo = new ProcessStartInfo
{
Arguments = options.Arguments,
FileName = options.FileName,
WorkingDirectory = options.WorkingDirectory,
UseShellExecute = options.UseShellExecute,
CreateNoWindow = options.CreateNoWindow,
RedirectStandardError = options.RedirectStandardError,
RedirectStandardInput = options.RedirectStandardInput,
RedirectStandardOutput = options.RedirectStandardOutput
};
startInfo.ErrorDialog = options.ErrorDialog;
if (options.IsHidden)
{
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
}
_process = new Process
{
StartInfo = startInfo
};
if (options.EnableRaisingEvents)
{
_process.EnableRaisingEvents = true;
_process.Exited += _process_Exited;
}
}
2018-09-12 17:26:21 +00:00
private bool _hasExited;
private bool HasExited
{
get
{
if (_hasExited)
{
return true;
}
try
{
_hasExited = _process.HasExited;
}
catch (InvalidOperationException)
{
_hasExited = true;
}
return _hasExited;
}
}
2016-11-01 04:07:12 +00:00
private void _process_Exited(object sender, EventArgs e)
{
2018-09-12 17:26:21 +00:00
_hasExited = true;
2016-11-01 04:07:12 +00:00
if (Exited != null)
{
2016-11-01 05:56:05 +00:00
Exited(this, e);
2016-11-01 04:07:12 +00:00
}
}
public ProcessOptions StartInfo => _options;
2016-11-01 04:07:12 +00:00
public StreamWriter StandardInput => _process.StandardInput;
2016-11-01 04:07:12 +00:00
public StreamReader StandardError => _process.StandardError;
2016-11-01 04:07:12 +00:00
public StreamReader StandardOutput => _process.StandardOutput;
2016-11-01 04:07:12 +00:00
public int ExitCode => _process.ExitCode;
2016-11-01 04:07:12 +00:00
public void Start()
{
_process.Start();
}
public void Kill()
{
_process.Kill();
}
public bool WaitForExit(int timeMs)
{
return _process.WaitForExit(timeMs);
}
2017-05-26 06:48:54 +00:00
public Task<bool> WaitForExitAsync(int timeMs)
{
2018-09-12 17:26:21 +00:00
//if (_process.WaitForExit(100))
//{
// return Task.FromResult(true);
//}
//timeMs -= 100;
timeMs = Math.Max(0, timeMs);
var tcs = new TaskCompletionSource<bool>();
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;
2017-05-26 06:48:54 +00:00
}
2016-11-01 04:07:12 +00:00
public void Dispose()
{
_process.Dispose();
}
}
}