CommonProcess.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Threading.Tasks;
  5. using MediaBrowser.Model.Diagnostics;
  6. namespace Emby.Server.Implementations.Diagnostics
  7. {
  8. public class CommonProcess : IProcess
  9. {
  10. public event EventHandler Exited;
  11. private readonly ProcessOptions _options;
  12. private readonly Process _process;
  13. public CommonProcess(ProcessOptions options)
  14. {
  15. _options = options;
  16. var startInfo = new ProcessStartInfo
  17. {
  18. Arguments = options.Arguments,
  19. FileName = options.FileName,
  20. WorkingDirectory = options.WorkingDirectory,
  21. UseShellExecute = options.UseShellExecute,
  22. CreateNoWindow = options.CreateNoWindow,
  23. RedirectStandardError = options.RedirectStandardError,
  24. RedirectStandardInput = options.RedirectStandardInput,
  25. RedirectStandardOutput = options.RedirectStandardOutput
  26. };
  27. startInfo.ErrorDialog = options.ErrorDialog;
  28. if (options.IsHidden)
  29. {
  30. startInfo.WindowStyle = ProcessWindowStyle.Hidden;
  31. }
  32. _process = new Process
  33. {
  34. StartInfo = startInfo
  35. };
  36. if (options.EnableRaisingEvents)
  37. {
  38. _process.EnableRaisingEvents = true;
  39. _process.Exited += _process_Exited;
  40. }
  41. }
  42. private void _process_Exited(object sender, EventArgs e)
  43. {
  44. if (Exited != null)
  45. {
  46. Exited(this, e);
  47. }
  48. }
  49. public ProcessOptions StartInfo
  50. {
  51. get { return _options; }
  52. }
  53. public StreamWriter StandardInput
  54. {
  55. get { return _process.StandardInput; }
  56. }
  57. public StreamReader StandardError
  58. {
  59. get { return _process.StandardError; }
  60. }
  61. public StreamReader StandardOutput
  62. {
  63. get { return _process.StandardOutput; }
  64. }
  65. public int ExitCode
  66. {
  67. get { return _process.ExitCode; }
  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. return Task.FromResult(_process.WaitForExit(timeMs));
  84. }
  85. public void Dispose()
  86. {
  87. _process.Dispose();
  88. GC.SuppressFinalize(this);
  89. }
  90. }
  91. }