ProcessBar.cs 7.5 KB

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