ProcessHandler.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Drawing;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows.Forms;
  9. using System.Runtime.InteropServices;
  10. namespace ServerControl
  11. {
  12. class ProcessHandler
  13. {
  14. Process myProcess = new Process();
  15. public Boolean isExecuted = false;
  16. public Boolean isHidden = false;
  17. Label statusLabel = new Label();
  18. RichTextBox outputText = new RichTextBox();
  19. [DllImport("User32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
  20. public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
  21. public ProcessHandler(string Path, string Command, Label Status, RichTextBox Output, Boolean noWindow = true)
  22. {
  23. statusLabel = Status;
  24. outputText = Output == null ? outputText : Output;
  25. myProcess.StartInfo.FileName = Path;
  26. myProcess.StartInfo.Arguments = Command;
  27. myProcess.StartInfo.RedirectStandardError = noWindow;
  28. myProcess.StartInfo.RedirectStandardOutput = noWindow;
  29. myProcess.StartInfo.RedirectStandardInput = noWindow;
  30. myProcess.StartInfo.UseShellExecute = false;
  31. myProcess.EnableRaisingEvents = true;
  32. myProcess.StartInfo.CreateNoWindow = noWindow;
  33. myProcess.OutputDataReceived += new DataReceivedEventHandler((s, e) =>
  34. {
  35. Console.WriteLine(e.Data);
  36. outputText.Invoke((Action)delegate { outputText.Text = outputText.Text + '\n' + e.Data; });
  37. });
  38. myProcess.ErrorDataReceived += new DataReceivedEventHandler((s, e) =>
  39. {
  40. Console.WriteLine(e.Data);
  41. outputText.Invoke((Action)delegate { outputText.Text = outputText.Text + '\n' + e.Data; });
  42. });
  43. myProcess.Exited += Disconnect;
  44. }
  45. public void Connect()
  46. {
  47. if (isExecuted) return;
  48. myProcess.Start();
  49. try
  50. {
  51. myProcess.BeginOutputReadLine();
  52. myProcess.BeginErrorReadLine();
  53. }
  54. catch (Exception) { }
  55. statusLabel.Invoke((Action)delegate { outputText.Text = ""; });
  56. statusLabel.Invoke((Action)delegate { statusLabel.Text = "Подключено"; });
  57. isExecuted = true;
  58. }
  59. public void Disconnect(object sender, EventArgs e)
  60. {
  61. Disconnect();
  62. }
  63. public void Disconnect()
  64. {
  65. if (!isExecuted) return;
  66. try
  67. {
  68. //myProcess.StandardInput.Close();
  69. myProcess.Kill();
  70. }
  71. catch (Exception e) { Console.WriteLine(e.Message); }
  72. try
  73. {
  74. myProcess.CancelOutputRead();
  75. myProcess.CancelErrorRead();
  76. }
  77. catch (Exception e) { Console.WriteLine(e.Message); }
  78. statusLabel.Invoke((Action)delegate { statusLabel.Text = "Отключено"; });
  79. isExecuted = false;
  80. }
  81. public void Hide()
  82. {
  83. isHidden = true;
  84. ShowWindow(myProcess.MainWindowHandle, 0);
  85. }
  86. public void Show()
  87. {
  88. isHidden = false;
  89. ShowWindow(myProcess.MainWindowHandle, 1);
  90. }
  91. }
  92. }