CommonProcess.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using MediaBrowser.Model.Diagnostics;
  8. namespace Emby.Common.Implementations.Diagnostics
  9. {
  10. public class CommonProcess : IProcess
  11. {
  12. public event EventHandler Exited;
  13. private readonly ProcessOptions _options;
  14. private readonly Process _process;
  15. public CommonProcess(ProcessOptions options)
  16. {
  17. _options = 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. };
  29. #if NET46
  30. startInfo.ErrorDialog = options.ErrorDialog;
  31. if (options.IsHidden)
  32. {
  33. startInfo.WindowStyle = ProcessWindowStyle.Hidden;
  34. }
  35. #endif
  36. _process = new Process
  37. {
  38. StartInfo = startInfo
  39. };
  40. if (options.EnableRaisingEvents)
  41. {
  42. _process.EnableRaisingEvents = true;
  43. _process.Exited += _process_Exited;
  44. }
  45. }
  46. private void _process_Exited(object sender, EventArgs e)
  47. {
  48. if (Exited != null)
  49. {
  50. Exited(_process, e);
  51. }
  52. }
  53. public ProcessOptions StartInfo
  54. {
  55. get { return _options; }
  56. }
  57. public StreamWriter StandardInput
  58. {
  59. get { return _process.StandardInput; }
  60. }
  61. public StreamReader StandardError
  62. {
  63. get { return _process.StandardError; }
  64. }
  65. public StreamReader StandardOutput
  66. {
  67. get { return _process.StandardOutput; }
  68. }
  69. public int ExitCode
  70. {
  71. get { return _process.ExitCode; }
  72. }
  73. public void Start()
  74. {
  75. _process.Start();
  76. }
  77. public void Kill()
  78. {
  79. _process.Kill();
  80. }
  81. public bool WaitForExit(int timeMs)
  82. {
  83. return _process.WaitForExit(timeMs);
  84. }
  85. public void Dispose()
  86. {
  87. _process.Dispose();
  88. }
  89. }
  90. }