CommonProcess.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #pragma warning disable CS1591
  2. #pragma warning disable SA1600
  3. using System;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using MediaBrowser.Model.Diagnostics;
  9. namespace Emby.Server.Implementations.Diagnostics
  10. {
  11. public class CommonProcess : IProcess
  12. {
  13. private readonly Process _process;
  14. private bool _disposed = false;
  15. private bool _hasExited;
  16. public CommonProcess(ProcessOptions options)
  17. {
  18. StartInfo = options;
  19. var startInfo = new ProcessStartInfo
  20. {
  21. Arguments = options.Arguments,
  22. FileName = options.FileName,
  23. WorkingDirectory = options.WorkingDirectory,
  24. UseShellExecute = options.UseShellExecute,
  25. CreateNoWindow = options.CreateNoWindow,
  26. RedirectStandardError = options.RedirectStandardError,
  27. RedirectStandardInput = options.RedirectStandardInput,
  28. RedirectStandardOutput = options.RedirectStandardOutput,
  29. ErrorDialog = options.ErrorDialog
  30. };
  31. if (options.IsHidden)
  32. {
  33. startInfo.WindowStyle = ProcessWindowStyle.Hidden;
  34. }
  35. _process = new Process
  36. {
  37. StartInfo = startInfo
  38. };
  39. if (options.EnableRaisingEvents)
  40. {
  41. _process.EnableRaisingEvents = true;
  42. _process.Exited += OnProcessExited;
  43. }
  44. }
  45. public event EventHandler Exited;
  46. public ProcessOptions StartInfo { get; }
  47. public StreamWriter StandardInput => _process.StandardInput;
  48. public StreamReader StandardError => _process.StandardError;
  49. public StreamReader StandardOutput => _process.StandardOutput;
  50. public int ExitCode => _process.ExitCode;
  51. private bool HasExited
  52. {
  53. get
  54. {
  55. if (_hasExited)
  56. {
  57. return true;
  58. }
  59. try
  60. {
  61. _hasExited = _process.HasExited;
  62. }
  63. catch (InvalidOperationException)
  64. {
  65. _hasExited = true;
  66. }
  67. return _hasExited;
  68. }
  69. }
  70. public void Start()
  71. {
  72. _process.Start();
  73. }
  74. public void Kill()
  75. {
  76. _process.Kill();
  77. }
  78. public bool WaitForExit(int timeMs)
  79. {
  80. return _process.WaitForExit(timeMs);
  81. }
  82. public Task<bool> WaitForExitAsync(int timeMs)
  83. {
  84. // Note: For this function to work correctly, the option EnableRisingEvents needs to be set to true.
  85. if (HasExited)
  86. {
  87. return Task.FromResult(true);
  88. }
  89. timeMs = Math.Max(0, timeMs);
  90. var tcs = new TaskCompletionSource<bool>();
  91. var cancellationToken = new CancellationTokenSource(timeMs).Token;
  92. _process.Exited += (sender, args) => tcs.TrySetResult(true);
  93. cancellationToken.Register(() => tcs.TrySetResult(HasExited));
  94. return tcs.Task;
  95. }
  96. public void Dispose()
  97. {
  98. Dispose(true);
  99. GC.SuppressFinalize(this);
  100. }
  101. protected virtual void Dispose(bool disposing)
  102. {
  103. if (_disposed)
  104. {
  105. return;
  106. }
  107. if (disposing)
  108. {
  109. _process?.Dispose();
  110. }
  111. _disposed = true;
  112. }
  113. private void OnProcessExited(object sender, EventArgs e)
  114. {
  115. _hasExited = true;
  116. Exited?.Invoke(this, e);
  117. }
  118. }
  119. }