CommonProcess.cs 3.7 KB

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