FrmLoading.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 2019-09-26
  4. //
  5. // ***********************************************************************
  6. // <copyright file="FrmLoading.cs">
  7. // Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
  8. // </copyright>
  9. //
  10. // Blog: https://www.cnblogs.com/bfyx
  11. // GitHub:https://github.com/kwwwvagaa/NetWinformControl
  12. // gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
  13. //
  14. // If you use this code, please keep this note.
  15. // ***********************************************************************
  16. using System;
  17. using System.Collections.Generic;
  18. using System.ComponentModel;
  19. using System.Data;
  20. using System.Drawing;
  21. using System.Linq;
  22. using System.Text;
  23. using System.Threading;
  24. using System.Windows.Forms;
  25. namespace HZH_Controls.Forms
  26. {
  27. /// <summary>
  28. /// Class FrmLoading.
  29. /// Implements the <see cref="HZH_Controls.Forms.FrmBase" />
  30. /// </summary>
  31. /// <seealso cref="HZH_Controls.Forms.FrmBase" />
  32. public partial class FrmLoading : FrmBase
  33. {
  34. /// <summary>
  35. /// The update database worker
  36. /// </summary>
  37. BackgroundWorker updateDBWorker = new BackgroundWorker();
  38. /// <summary>
  39. /// 获取或设置加载任务
  40. /// </summary>
  41. /// <value>The background work action.</value>
  42. public Action BackgroundWorkAction
  43. {
  44. get;
  45. set;
  46. }
  47. /// <summary>
  48. /// 设置当前执行进度及任务名称,key:任务进度,取值0-100 value:当前任务名称
  49. /// </summary>
  50. /// <value>The current MSG.</value>
  51. public KeyValuePair<int, string> CurrentMsg
  52. {
  53. set
  54. {
  55. this.updateDBWorker.ReportProgress(value.Key, value.Value);
  56. }
  57. }
  58. /// <summary>
  59. /// Initializes a new instance of the <see cref="FrmLoading"/> class.
  60. /// </summary>
  61. public FrmLoading()
  62. {
  63. InitializeComponent();
  64. this.updateDBWorker.WorkerReportsProgress = true;
  65. this.updateDBWorker.WorkerSupportsCancellation = true;
  66. this.updateDBWorker.DoWork += new DoWorkEventHandler(this.backgroundWorker1_DoWork);
  67. this.updateDBWorker.ProgressChanged += new ProgressChangedEventHandler(this.backgroundWorker1_ProgressChanged);
  68. }
  69. /// <summary>
  70. /// 设置进度信息,重写此函数可以处理界面信息绑定
  71. /// </summary>
  72. /// <param name="strText">进度任务名称</param>
  73. /// <param name="intValue">进度值</param>
  74. protected virtual void BindingProcessMsg(string strText, int intValue)
  75. {
  76. }
  77. /// <summary>
  78. /// Sets the message.
  79. /// </summary>
  80. /// <param name="strText">The string text.</param>
  81. /// <param name="intValue">The int value.</param>
  82. private void SetMessage(string strText, int intValue)
  83. {
  84. if (this.InvokeRequired)
  85. {
  86. this.BeginInvoke(new MethodInvoker(delegate() { SetMessage(strText, intValue); }));
  87. }
  88. else
  89. {
  90. BindingProcessMsg(strText, intValue);
  91. }
  92. }
  93. /// <summary>
  94. /// Handles the Load event of the FrmLoading control.
  95. /// </summary>
  96. /// <param name="sender">The source of the event.</param>
  97. /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  98. private void FrmLoading_Load(object sender, EventArgs e)
  99. {
  100. if (ControlHelper.IsDesignMode())
  101. return;
  102. this.updateDBWorker.RunWorkerAsync();
  103. }
  104. /// <summary>
  105. /// Handles the DoWork event of the backgroundWorker1 control.
  106. /// </summary>
  107. /// <param name="sender">The source of the event.</param>
  108. /// <param name="e">The <see cref="DoWorkEventArgs"/> instance containing the event data.</param>
  109. private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
  110. {
  111. if (this.BackgroundWorkAction != null)
  112. {
  113. this.BackgroundWorkAction();
  114. }
  115. Thread.Sleep(100);
  116. if (base.InvokeRequired)
  117. {
  118. base.BeginInvoke(new MethodInvoker(delegate
  119. {
  120. base.Close();
  121. }));
  122. }
  123. else
  124. {
  125. base.Close();
  126. }
  127. }
  128. /// <summary>
  129. /// Handles the ProgressChanged event of the backgroundWorker1 control.
  130. /// </summary>
  131. /// <param name="sender">The source of the event.</param>
  132. /// <param name="e">The <see cref="ProgressChangedEventArgs"/> instance containing the event data.</param>
  133. private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
  134. {
  135. SetMessage((e.UserState == null) ? "" : e.UserState.ToString(), e.ProgressPercentage);
  136. }
  137. }
  138. }