ProcessBar.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using System;
  2. using System.Drawing;
  3. using System.Threading;
  4. using System.Windows.Forms;
  5. namespace UAS_MES_NEW.CustomControl
  6. {
  7. public partial class ProcessBar : UserControl
  8. {
  9. public delegate void TaskHandler(ref float percentage);
  10. private Thread _monitorThread = null;
  11. private Thread _workerThread = null;
  12. private float _percentage = 0f;
  13. private ProcessBarStatue _currentStatue = ProcessBarStatue.UnStarted;
  14. private TaskHandler _task = null;
  15. public ProcessBar()
  16. {
  17. InitializeComponent();
  18. try { UAS_MES_NEW.I18n.LanguageManager.Localize(this); } catch { }
  19. this._monitorThread = new Thread(new ThreadStart(Monitor));
  20. }
  21. /// <summary>
  22. /// 任务委托
  23. /// 多次对该属性赋值会造成不必要的浪费
  24. /// </summary>
  25. public TaskHandler Task
  26. {
  27. set
  28. {
  29. //任务线程 通过传入引用类型参数获得实时的进度变更
  30. this._workerThread = new Thread(() => value(ref _percentage));
  31. this._task = value;
  32. }
  33. }
  34. /// <summary>
  35. /// 外部也可以实时获取当前任务进度
  36. /// </summary>
  37. public float Percentage
  38. {
  39. get
  40. {
  41. return this._percentage;
  42. }
  43. }
  44. /// <summary>
  45. /// 外部实时获取当前任务状态
  46. /// </summary>
  47. public ProcessBarStatue CurrentStatue
  48. {
  49. get
  50. {
  51. return this._currentStatue;
  52. }
  53. }
  54. public void Run()
  55. {
  56. if (this._workerThread == null)
  57. {
  58. throw new NullReferenceException("Task委托不能为空!");
  59. }
  60. //初次启动 需要同时启动工作线程和监听线程
  61. if (this._currentStatue == ProcessBarStatue.UnStarted)
  62. {
  63. _monitorThread.IsBackground = true;
  64. _monitorThread.Start();
  65. _workerThread.IsBackground = true;
  66. _workerThread.Start();
  67. this._currentStatue = ProcessBarStatue.Running;
  68. }
  69. //被终止后第二次启动 只需要重启工作线程就可以
  70. else if (this._currentStatue == ProcessBarStatue.Aborted)
  71. {
  72. this._currentStatue = ProcessBarStatue.Running;
  73. //若原工作线程已经终止 则重新初始化工作线程
  74. if (this._workerThread.ThreadState == ThreadState.Aborted)
  75. {
  76. this._workerThread = null;
  77. this._workerThread = new Thread(() => _task(ref _percentage));
  78. }
  79. _workerThread.IsBackground = true;
  80. _workerThread.Start();
  81. }
  82. else
  83. {
  84. throw new InvalidOperationException("已经开始的任务无法再次开始!");
  85. }
  86. }
  87. public void Stop()
  88. {
  89. if (_workerThread == null)
  90. {
  91. throw new NullReferenceException("Task委托不能为空!");
  92. }
  93. if (this._currentStatue == ProcessBarStatue.Aborted)
  94. {
  95. throw new InvalidOperationException("已经终止的操作无法暂停!");
  96. }
  97. if (this._currentStatue != ProcessBarStatue.Suspended)
  98. {
  99. //_workerThread.Suspend();
  100. //_monitorThread.Suspend();
  101. this._currentStatue = ProcessBarStatue.Suspended;
  102. }
  103. }
  104. public void Resume()
  105. {
  106. if (_workerThread == null)
  107. {
  108. throw new NullReferenceException("Task委托不能为空!");
  109. }
  110. if (this._currentStatue == ProcessBarStatue.Aborted)
  111. {
  112. throw new InvalidOperationException("已经终止的操作无法继续!");
  113. }
  114. if (this._currentStatue == ProcessBarStatue.Suspended)
  115. {
  116. //_monitorThread.Resume();
  117. //_workerThread.Resume();
  118. this._currentStatue = ProcessBarStatue.Running;
  119. }
  120. }
  121. public void Abort()
  122. {
  123. if (this._workerThread == null)
  124. {
  125. throw new NullReferenceException("Task委托不能为空!");
  126. }
  127. if (this._currentStatue != ProcessBarStatue.Aborted)
  128. {
  129. //若之前为Suspended状态 则需要先Resume才可以终止工作线程
  130. this.Resume();
  131. _workerThread.Abort();
  132. this._currentStatue = ProcessBarStatue.Aborted;
  133. }
  134. }
  135. private void Monitor()
  136. {
  137. //外层无限循环 监听标志变量的改变
  138. while (true)
  139. {
  140. //当UI调用Start方法时监听线程首次启动并设置标志变量为Running
  141. if (this._currentStatue == ProcessBarStatue.Running)
  142. {
  143. //在此循环中监听_percetage变量的改变情况 由于该变量作为引用参数传入Task委托 所以可以得到实时的进度
  144. while (this._percentage <= 1f)
  145. {
  146. //调用Draw方法更新UI
  147. using (var graphic = pictureBox1.CreateGraphics())
  148. {
  149. if (this.pictureBox1.InvokeRequired)
  150. {
  151. this.Invoke(new Action(() => Draw(this._percentage, graphic)));
  152. }
  153. else
  154. {
  155. this.Draw(this._percentage, graphic);
  156. }
  157. Thread.Sleep(10);//重绘间隔为10ms
  158. //当发现标志变量改变为终止时跳出 回到最外层监听标志变量的循环
  159. if (this._currentStatue == ProcessBarStatue.Aborted)
  160. {
  161. this._percentage = 0f;//将百分比重置为0
  162. if (this.pictureBox1.InvokeRequired)//将绘图区清空
  163. {
  164. this.Invoke(new Action(() => this.Clear(graphic)));
  165. }
  166. else
  167. {
  168. this.Clear(graphic);
  169. }
  170. break;
  171. }
  172. }
  173. }
  174. }
  175. }
  176. }
  177. /// <summary>
  178. /// 如果要更改进度条显示的形式 则继承当前类 重写Draw方法
  179. /// </summary>
  180. /// <param name="percentage"></param>
  181. /// <param name="graphic"></param>
  182. protected virtual void Draw(float percentage, Graphics graphic)
  183. {
  184. graphic.FillRectangle(Brushes.Black, 0f, 0f, _percentage * pictureBox1.Width, pictureBox1.Height);
  185. }
  186. /// <summary>
  187. /// 若需要更改进度条默认颜色 背景 则继承当前类 重写Clear方法
  188. /// </summary>
  189. /// <param name="graphic"></param>
  190. private void Clear(Graphics graphic)
  191. {
  192. graphic.Clear(Color.White);
  193. }
  194. public enum ProcessBarStatue
  195. {
  196. UnStarted,
  197. Running,
  198. Suspended,
  199. Aborted
  200. }
  201. }
  202. }