CommonProcess.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. public event EventHandler Exited;
  12. private readonly ProcessOptions _options;
  13. private readonly Process _process;
  14. public CommonProcess(ProcessOptions options)
  15. {
  16. _options = 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. };
  28. startInfo.ErrorDialog = options.ErrorDialog;
  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 += _process_Exited;
  41. }
  42. }
  43. private bool _hasExited;
  44. private bool HasExited
  45. {
  46. get
  47. {
  48. if (_hasExited)
  49. {
  50. return true;
  51. }
  52. try
  53. {
  54. _hasExited = _process.HasExited;
  55. }
  56. catch (InvalidOperationException)
  57. {
  58. _hasExited = true;
  59. }
  60. return _hasExited;
  61. }
  62. }
  63. private void _process_Exited(object sender, EventArgs e)
  64. {
  65. _hasExited = true;
  66. if (Exited != null)
  67. {
  68. Exited(this, e);
  69. }
  70. }
  71. public ProcessOptions StartInfo => _options;
  72. public StreamWriter StandardInput => _process.StandardInput;
  73. public StreamReader StandardError => _process.StandardError;
  74. public StreamReader StandardOutput => _process.StandardOutput;
  75. public int ExitCode => _process.ExitCode;
  76. public void Start()
  77. {
  78. _process.Start();
  79. }
  80. public void Kill()
  81. {
  82. _process.Kill();
  83. }
  84. public bool WaitForExit(int timeMs)
  85. {
  86. return _process.WaitForExit(timeMs);
  87. }
  88. public Task<bool> WaitForExitAsync(int timeMs)
  89. {
  90. //Note: For this function to work correctly, the option EnableRisingEvents needs to be set to true.
  91. if (HasExited)
  92. {
  93. return Task.FromResult(true);
  94. }
  95. timeMs = Math.Max(0, timeMs);
  96. var tcs = new TaskCompletionSource<bool>();
  97. var cancellationToken = new CancellationTokenSource(timeMs).Token;
  98. _process.Exited += (sender, args) => tcs.TrySetResult(true);
  99. cancellationToken.Register(() => tcs.TrySetResult(HasExited));
  100. return tcs.Task;
  101. }
  102. public void Dispose()
  103. {
  104. _process.Dispose();
  105. }
  106. }
  107. }