CommonProcess.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Threading.Tasks;
  5. using MediaBrowser.Model.Diagnostics;
  6. using System.Threading;
  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
  72. {
  73. get { return _options; }
  74. }
  75. public StreamWriter StandardInput
  76. {
  77. get { return _process.StandardInput; }
  78. }
  79. public StreamReader StandardError
  80. {
  81. get { return _process.StandardError; }
  82. }
  83. public StreamReader StandardOutput
  84. {
  85. get { return _process.StandardOutput; }
  86. }
  87. public int ExitCode
  88. {
  89. get { return _process.ExitCode; }
  90. }
  91. public void Start()
  92. {
  93. _process.Start();
  94. }
  95. public void Kill()
  96. {
  97. _process.Kill();
  98. }
  99. public bool WaitForExit(int timeMs)
  100. {
  101. return _process.WaitForExit(timeMs);
  102. }
  103. public Task<bool> WaitForExitAsync(int timeMs)
  104. {
  105. //if (_process.WaitForExit(100))
  106. //{
  107. // return Task.FromResult(true);
  108. //}
  109. //timeMs -= 100;
  110. timeMs = Math.Max(0, timeMs);
  111. var tcs = new TaskCompletionSource<bool>();
  112. var cancellationToken = new CancellationTokenSource(timeMs).Token;
  113. if (HasExited)
  114. {
  115. return Task.FromResult(true);
  116. }
  117. _process.Exited += (sender, args) => tcs.TrySetResult(true);
  118. cancellationToken.Register(() => tcs.TrySetResult(HasExited));
  119. return tcs.Task;
  120. }
  121. public void Dispose()
  122. {
  123. _process.Dispose();
  124. }
  125. }
  126. }