FrmLoadHandling.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Windows.Forms;
  10. namespace LYFZ.ComponentLibrary
  11. {
  12. /// <summary>
  13. /// 进度状态显示类型
  14. /// </summary>
  15. public enum LoadHandlingType {
  16. /// <summary>
  17. /// 加载固定状态显示,只显示进度动画,不显示具体进度值
  18. /// </summary>
  19. LoadProgress,
  20. /// <summary>
  21. /// 执行进度条显示,显示具体进度值
  22. /// </summary>
  23. ExecutionProgress,
  24. }
  25. public delegate void ExecutionBackgroundDoWork(object argument, BackgroundWorker backgroundWorker);
  26. public delegate void LoadBackgroundDoWork(object argument, BackgroundWorker backgroundWorker);
  27. /// <summary>
  28. /// 程序在执行时显示的进度状态窗体
  29. /// </summary>
  30. public partial class FrmLoadHandling : Form
  31. {
  32. /// <summary>
  33. ///
  34. /// </summary>
  35. private BackgroundWorker backgroundWorker = new BackgroundWorker();
  36. /// <summary>
  37. /// 独立执行线程
  38. /// </summary>
  39. public BackgroundWorker BackgroundWorker
  40. {
  41. get { return backgroundWorker; }
  42. set { backgroundWorker = value; }
  43. }
  44. /// <summary>
  45. ///
  46. /// </summary>
  47. private ExecutionBackgroundDoWork executionBackgroundDoWorkMethod = null;
  48. /// <summary>
  49. /// ExecutionProgress要执行的方法委托,设置要执行的法
  50. /// </summary>
  51. public ExecutionBackgroundDoWork ExecutionBackgroundDoWorkMethod
  52. {
  53. get { return executionBackgroundDoWorkMethod; }
  54. set { executionBackgroundDoWorkMethod = value; }
  55. }
  56. /// <summary>
  57. ///
  58. /// </summary>
  59. private LoadBackgroundDoWork loadBackgroundDoWorkMethod = null;
  60. /// <summary>
  61. /// LoadProgress要执行的方法委托,设置要执行的法
  62. /// </summary>
  63. public LoadBackgroundDoWork LoadBackgroundDoWorkMethod
  64. {
  65. get { return loadBackgroundDoWorkMethod; }
  66. set { loadBackgroundDoWorkMethod = value; }
  67. }
  68. LoadProgress loadProgress = new LoadProgress();
  69. /// <summary>
  70. /// LoadProgress固定滚动进度条
  71. /// </summary>
  72. public LoadProgress LoadProgressControl
  73. {
  74. get { return loadProgress; }
  75. set { loadProgress = value; }
  76. }
  77. ExecutionProgress executionProgress = new ExecutionProgress();
  78. /// <summary>
  79. /// ExecutionProgress进度值滚动进度条
  80. /// </summary>
  81. public ExecutionProgress ExecutionProgressControl
  82. {
  83. get { return executionProgress; }
  84. set { executionProgress = value; }
  85. }
  86. ManualResetEvent eventDone = new ManualResetEvent(false);
  87. /// <summary>
  88. ///
  89. /// </summary>
  90. public ManualResetEvent EventDone
  91. {
  92. get { return eventDone; }
  93. set { eventDone = value; }
  94. }
  95. public FrmLoadHandling(bool isInitializeBackgroundWorker)
  96. {
  97. this.isInitializeBackgroundWorker = isInitializeBackgroundWorker;
  98. InitializeBackgroundWorker(LoadHandlingType.LoadProgress);
  99. }
  100. public FrmLoadHandling()
  101. {
  102. InitializeBackgroundWorker(LoadHandlingType.LoadProgress);
  103. }
  104. LoadHandlingType _type = LoadHandlingType.LoadProgress;
  105. public FrmLoadHandling(LoadHandlingType type)
  106. {
  107. InitializeBackgroundWorker(type);
  108. }
  109. bool isInitializeBackgroundWorker = true;
  110. /// <summary>
  111. /// 初始化独立线程
  112. /// </summary>
  113. public void InitializeBackgroundWorker(LoadHandlingType type)
  114. {
  115. InitializeComponent();
  116. this._type = type;
  117. switch (this._type)
  118. {
  119. case LoadHandlingType.LoadProgress:
  120. loadProgress.Location = new Point(0,0);
  121. loadProgress.Dock = DockStyle.Fill;
  122. loadProgress.Name = "load" + DateTime.Now.ToString("yyyyMMddhhmmssff");
  123. this.Controls.Add(loadProgress);
  124. break;
  125. case LoadHandlingType.ExecutionProgress:
  126. this.Width = 280;
  127. executionProgress.Location = new Point(0, 0);
  128. executionProgress.Dock = DockStyle.Fill;
  129. executionProgress.Name = "exe" + DateTime.Now.ToString("yyyyMMddhhmmssff");
  130. this.Controls.Add(executionProgress);
  131. break;
  132. }
  133. if (isInitializeBackgroundWorker)
  134. {
  135. this.Load += new System.EventHandler(this.FrmLoadHandling_Load);
  136. backgroundWorker.WorkerReportsProgress = true;
  137. backgroundWorker.WorkerSupportsCancellation = true;
  138. backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);
  139. backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker_ProgressChanged);
  140. backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker_RunWorkerCompleted);
  141. }
  142. }
  143. void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  144. {
  145. try
  146. {
  147. if (e.Cancelled)
  148. {
  149. this.EventDone.Set();
  150. this.DialogResult = DialogResult.Cancel;
  151. // MessageBox.Show("操作取消");
  152. }
  153. else
  154. {
  155. this.EventDone.Set();
  156. this.DialogResult = DialogResult.OK;
  157. // MessageBox.Show("操作完成");
  158. }
  159. }
  160. catch {
  161. this.EventDone.Set();
  162. this.DialogResult = DialogResult.Cancel;
  163. }
  164. }
  165. void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
  166. {
  167. try
  168. {
  169. if (e.ProgressPercentage == -1)
  170. {
  171. this.Opacity = 0;
  172. return;
  173. }
  174. else
  175. {
  176. this.Opacity = 100;
  177. }
  178. if (this._type == LoadHandlingType.ExecutionProgress)
  179. {
  180. if (e.ProgressPercentage >= executionProgress.MySkinProgressBar.Maximum)
  181. {
  182. executionProgress.MySkinProgressBarValue = executionProgress.MySkinProgressBar.Maximum;
  183. }
  184. else
  185. {
  186. executionProgress.MySkinProgressBarValue = e.ProgressPercentage;
  187. }
  188. if (e.UserState != null)
  189. {
  190. ExecutionProgressControl.LbLoadProgress.Text = e.UserState.ToString();
  191. }
  192. }
  193. if (this._type == LoadHandlingType.LoadProgress)
  194. {
  195. if (e.UserState != null)
  196. {
  197. LoadProgressControl.LbLoadProgress.Text = e.UserState.ToString();
  198. }
  199. }
  200. }
  201. catch { }
  202. }
  203. void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
  204. {
  205. try
  206. {
  207. Form frmP = this.ParentForm;
  208. switch (this._type)
  209. {
  210. case LoadHandlingType.ExecutionProgress:
  211. if (ExecutionBackgroundDoWorkMethod != null)
  212. {
  213. ExecutionBackgroundDoWorkMethod(e.Argument, this.BackgroundWorker);
  214. e.Result = true;
  215. }
  216. else
  217. {
  218. e.Result = false;
  219. }
  220. break;
  221. case LoadHandlingType.LoadProgress:
  222. if (LoadBackgroundDoWorkMethod != null)
  223. {
  224. LoadBackgroundDoWorkMethod(e.Argument, this.BackgroundWorker);
  225. e.Result = true;
  226. }
  227. else
  228. {
  229. e.Result = false;
  230. }
  231. break;
  232. }
  233. }
  234. catch (Exception ex){
  235. MessageBoxCustom.Show(ex.Message);
  236. }
  237. }
  238. /// <summary>
  239. /// 执行指定方法并显示固定状态进度条
  240. /// </summary>
  241. /// <param name="doWorkMethod">要执行指定的方法</param>
  242. /// <param name="argument">参数</param>
  243. /// <returns></returns>
  244. public static DialogResult LoadDoWorkMethod(LoadBackgroundDoWork doWorkMethod, object argument = null, int width = 190,bool isTopMost=false)
  245. {
  246. FrmLoadHandling frm = new FrmLoadHandling();
  247. frm.TopMost = isTopMost;
  248. if (width > 190 && width < 800)
  249. {
  250. frm.Width = width;
  251. }
  252. frm.LoadBackgroundDoWorkMethod = doWorkMethod;
  253. frm.BackgroundWorker.RunWorkerAsync(argument);
  254. return frm.ShowDialog();
  255. }
  256. /// <summary>
  257. /// 执行指定方法并显示具体进度值状态进度条
  258. /// </summary>
  259. /// <param name="doWorkMethod">要执行指定的方法</param>
  260. /// <param name="argument">参数</param>
  261. /// <param name="width">显示进度条窗体的宽度,值为:190~800 之间的整数</param>
  262. /// <returns></returns>
  263. public static DialogResult ExecutionDoWorkMethod(ExecutionBackgroundDoWork doWorkMethod, object argument = null, int width = 190)
  264. {
  265. FrmLoadHandling frm = new FrmLoadHandling(LoadHandlingType.ExecutionProgress);
  266. // frm.TopMost = true;
  267. if (width > 190 && width < 800)
  268. {
  269. frm.Width = width;
  270. }
  271. frm.ExecutionBackgroundDoWorkMethod= doWorkMethod;
  272. frm.BackgroundWorker.RunWorkerAsync(argument);
  273. return frm.ShowDialog();
  274. }
  275. private void FrmLoadHandling_Load(object sender, EventArgs e)
  276. {
  277. // backgroundWorker.RunWorkerAsync();
  278. }
  279. }
  280. }