ScriptExecutor.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. 
  2. using System;
  3. using System.Collections.Concurrent;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.IO;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. namespace AiControlRequest
  12. {
  13. /// <summary>
  14. /// 脚本执行类
  15. /// lbf 2020/05/20 14:18
  16. /// </summary>
  17. public abstract class ScriptExecutor
  18. {
  19. public DataReceivedEventHandler DataReceivedEvent;
  20. private ConcurrentQueue<DataReceivedEventArgs> concurrentQueue = new ConcurrentQueue<DataReceivedEventArgs>();
  21. /// <summary>
  22. /// 执行脚本的命令
  23. /// </summary>
  24. protected string ProcessName { get; set; }
  25. protected SynchronizationContext synchronizationContext = null;
  26. public event SendOrPostCallback ReceivedControlData;
  27. public ScriptExecutor(SynchronizationContext context,string ProcessName)
  28. {
  29. this.synchronizationContext = context;
  30. this.ProcessName = ProcessName;
  31. new Task(() => {
  32. DataReceivedEventArgs args;
  33. while (true)
  34. {
  35. if (this.concurrentQueue.TryDequeue(out args))
  36. {
  37. // 为异步获取订阅事件
  38. if (this.DataReceivedEvent != null)
  39. {
  40. this.DataReceivedEvent(this, args);
  41. }
  42. }
  43. else
  44. {
  45. Task.Delay(10);
  46. }
  47. }
  48. }).Start();
  49. }
  50. /// <summary>
  51. /// 根据脚本返回结果分类数据
  52. /// </summary>
  53. /// <param name="control">界面接收数据控件</param>
  54. /// <param name="e">异步返回数据</param>
  55. /// <returns>控件分类数据</returns>
  56. protected abstract ControlData produceControlData(Control control, DataReceivedEventArgs e);
  57. private void addDataReceivedEvent(Control control, string commandString)
  58. {
  59. if (DataReceivedEvent == null)
  60. {
  61. DataReceivedEvent += new DataReceivedEventHandler((sender1, e1) => {
  62. if (!string.IsNullOrEmpty(e1.Data))
  63. {
  64. if (this.ReceivedControlData != null)
  65. {
  66. this.synchronizationContext.Post(this.ReceivedControlData, produceControlData(control,e1));
  67. }
  68. }
  69. });
  70. }
  71. }
  72. /// <summary>
  73. /// 执行脚本命令,结果会通过ReceivedControlData事件返回
  74. /// </summary>
  75. /// <param name="control">接收数据的控件</param>
  76. /// <param name="commandString">执行的脚本命令</param>
  77. public void execute(Control control, string commandString)
  78. {
  79. addDataReceivedEvent(control, commandString);
  80. if (this.ReceivedControlData != null)
  81. {
  82. this.synchronizationContext.Post(this.ReceivedControlData, new ControlData { Control = control, CallbackDataType = CallbackDataType.Command, DataString = commandString });
  83. }
  84. this.executeScript(commandString);
  85. }
  86. public void DataReceivedEventHandler(object sender, DataReceivedEventArgs e)
  87. {
  88. //push into queue
  89. this.concurrentQueue.Enqueue(e);
  90. }
  91. /// <summary>
  92. /// 执行脚本,脚本返回数据在OutputDataReceived数据中返回,调用都需要挂载OutputDataReceived事件接收返回数据
  93. /// </summary>
  94. /// <param name="processName">脚本进程名</param>
  95. /// <param name="commandString">要执行的脚本文件+参数</param>
  96. private void executeScript( string commandString)
  97. {
  98. ProcessStartInfo start = new ProcessStartInfo();
  99. start.FileName = this.ProcessName;
  100. start.Arguments = commandString;
  101. start.UseShellExecute = false;
  102. start.RedirectStandardOutput = true;
  103. start.RedirectStandardInput = true;
  104. start.RedirectStandardError = true;
  105. start.CreateNoWindow = true;
  106. using (Process p = Process.Start(start))
  107. {
  108. // 异步获取命令行内容
  109. p.BeginOutputReadLine();
  110. // 为异步获取订阅事件
  111. if (this.DataReceivedEvent != null)
  112. {
  113. p.OutputDataReceived += DataReceivedEventHandler;
  114. ;
  115. }
  116. }
  117. }
  118. }
  119. }