FrmLoadHandling.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. try
  116. {
  117. InitializeComponent();
  118. this._type = type;
  119. switch (this._type)
  120. {
  121. case LoadHandlingType.LoadProgress:
  122. loadProgress.Location = new Point(0, 0);
  123. loadProgress.Dock = DockStyle.Fill;
  124. loadProgress.Name = "load" + DateTime.Now.ToString("yyyyMMddhhmmssff");
  125. this.Controls.Add(loadProgress);
  126. break;
  127. case LoadHandlingType.ExecutionProgress:
  128. this.Width = 280;
  129. executionProgress.Location = new Point(0, 0);
  130. executionProgress.Dock = DockStyle.Fill;
  131. executionProgress.Name = "exe" + DateTime.Now.ToString("yyyyMMddhhmmssff");
  132. this.Controls.Add(executionProgress);
  133. break;
  134. }
  135. if (isInitializeBackgroundWorker)
  136. {
  137. this.Load += new System.EventHandler(this.FrmLoadHandling_Load);
  138. backgroundWorker.WorkerReportsProgress = true;
  139. backgroundWorker.WorkerSupportsCancellation = true;
  140. backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);
  141. backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker_ProgressChanged);
  142. backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker_RunWorkerCompleted);
  143. }
  144. }
  145. catch { }
  146. }
  147. void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  148. {
  149. try
  150. {
  151. if (e.Cancelled)
  152. {
  153. this.EventDone.Set();
  154. this.DialogResult = DialogResult.Cancel;
  155. // MessageBox.Show("操作取消");
  156. }
  157. else
  158. {
  159. this.EventDone.Set();
  160. this.DialogResult = DialogResult.OK;
  161. // MessageBox.Show("操作完成");
  162. }
  163. }
  164. catch {
  165. this.EventDone.Set();
  166. this.DialogResult = DialogResult.Cancel;
  167. }
  168. }
  169. void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
  170. {
  171. try
  172. {
  173. if (e.ProgressPercentage == -1)
  174. {
  175. this.Opacity = 0;
  176. return;
  177. }
  178. else
  179. {
  180. this.Opacity = 100;
  181. }
  182. if (this._type == LoadHandlingType.ExecutionProgress)
  183. {
  184. if (e.ProgressPercentage >= executionProgress.MySkinProgressBar.Maximum)
  185. {
  186. executionProgress.MySkinProgressBarValue = executionProgress.MySkinProgressBar.Maximum;
  187. }
  188. else
  189. {
  190. executionProgress.MySkinProgressBarValue = e.ProgressPercentage;
  191. }
  192. if (e.UserState != null)
  193. {
  194. ExecutionProgressControl.LbLoadProgress.Text = e.UserState.ToString();
  195. }
  196. }
  197. if (this._type == LoadHandlingType.LoadProgress)
  198. {
  199. if (e.UserState != null)
  200. {
  201. LoadProgressControl.LbLoadProgress.Text = e.UserState.ToString();
  202. }
  203. }
  204. }
  205. catch { }
  206. }
  207. void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
  208. {
  209. //try
  210. //{
  211. Form frmP = this.ParentForm;
  212. switch (this._type)
  213. {
  214. case LoadHandlingType.ExecutionProgress:
  215. if (ExecutionBackgroundDoWorkMethod != null)
  216. {
  217. ExecutionBackgroundDoWorkMethod(e.Argument, this.BackgroundWorker);
  218. e.Result = true;
  219. }
  220. else
  221. {
  222. e.Result = false;
  223. }
  224. break;
  225. case LoadHandlingType.LoadProgress:
  226. if (LoadBackgroundDoWorkMethod != null)
  227. {
  228. LoadBackgroundDoWorkMethod(e.Argument, this.BackgroundWorker);
  229. e.Result = true;
  230. }
  231. else
  232. {
  233. e.Result = false;
  234. }
  235. break;
  236. }
  237. //}
  238. // catch (Exception ex){
  239. // MessageBoxCustom.Show(ex.Message);
  240. //}
  241. }
  242. /// <summary>
  243. /// 执行指定方法并显示固定状态进度条
  244. /// </summary>
  245. /// <param name="doWorkMethod">要执行指定的方法</param>
  246. /// <param name="argument">参数</param>
  247. /// <returns></returns>
  248. public static DialogResult LoadDoWorkMethod(LoadBackgroundDoWork doWorkMethod, object argument = null)
  249. {
  250. try
  251. {
  252. FrmLoadHandling frm = new FrmLoadHandling();
  253. // frm.TopMost = true;
  254. frm.LoadBackgroundDoWorkMethod = doWorkMethod;
  255. frm.BackgroundWorker.RunWorkerAsync(argument);
  256. return frm.ShowDialog();
  257. }
  258. catch {
  259. return DialogResult.Cancel;
  260. }
  261. }
  262. /// <summary>
  263. /// 执行指定方法并显示具体进度值状态进度条
  264. /// </summary>
  265. /// <param name="doWorkMethod">要执行指定的方法</param>
  266. /// <param name="argument">参数</param>
  267. /// <param name="width">显示进度条窗体的宽度,值为:190~800 之间的整数</param>
  268. /// <returns></returns>
  269. public static DialogResult ExecutionDoWorkMethod(ExecutionBackgroundDoWork doWorkMethod, object argument = null, int width = 190)
  270. {
  271. try
  272. {
  273. FrmLoadHandling frm = new FrmLoadHandling(LoadHandlingType.ExecutionProgress);
  274. // frm.TopMost = true;
  275. if (width > 190 && width < 800)
  276. {
  277. frm.Width = width;
  278. }
  279. frm.ExecutionBackgroundDoWorkMethod = doWorkMethod;
  280. frm.BackgroundWorker.RunWorkerAsync(argument);
  281. return frm.ShowDialog();
  282. }
  283. catch {
  284. return DialogResult.Cancel;
  285. }
  286. }
  287. private void FrmLoadHandling_Load(object sender, EventArgs e)
  288. {
  289. // backgroundWorker.RunWorkerAsync();
  290. }
  291. }
  292. }