CommonProcess.cs 3.7 KB

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