// *********************************************************************** // Assembly : HZH_Controls // Created : 2019-09-26 // // *********************************************************************** // // Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com // // // Blog: https://www.cnblogs.com/bfyx // GitHub:https://github.com/kwwwvagaa/NetWinformControl // gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git // // If you use this code, please keep this note. // *********************************************************************** using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Windows.Forms; namespace HZH_Controls.Forms { /// /// Class FrmLoading. /// Implements the /// /// public partial class FrmLoading : FrmBase { /// /// The update database worker /// BackgroundWorker updateDBWorker = new BackgroundWorker(); /// /// 获取或设置加载任务 /// /// The background work action. public Action BackgroundWorkAction { get; set; } /// /// 设置当前执行进度及任务名称,key:任务进度,取值0-100 value:当前任务名称 /// /// The current MSG. public KeyValuePair CurrentMsg { set { this.updateDBWorker.ReportProgress(value.Key, value.Value); } } /// /// Initializes a new instance of the class. /// public FrmLoading() { InitializeComponent(); this.updateDBWorker.WorkerReportsProgress = true; this.updateDBWorker.WorkerSupportsCancellation = true; this.updateDBWorker.DoWork += new DoWorkEventHandler(this.backgroundWorker1_DoWork); this.updateDBWorker.ProgressChanged += new ProgressChangedEventHandler(this.backgroundWorker1_ProgressChanged); } /// /// 设置进度信息,重写此函数可以处理界面信息绑定 /// /// 进度任务名称 /// 进度值 protected virtual void BindingProcessMsg(string strText, int intValue) { } /// /// Sets the message. /// /// The string text. /// The int value. private void SetMessage(string strText, int intValue) { if (this.InvokeRequired) { this.BeginInvoke(new MethodInvoker(delegate() { SetMessage(strText, intValue); })); } else { BindingProcessMsg(strText, intValue); } } /// /// Handles the Load event of the FrmLoading control. /// /// The source of the event. /// The instance containing the event data. private void FrmLoading_Load(object sender, EventArgs e) { if (ControlHelper.IsDesignMode()) return; this.updateDBWorker.RunWorkerAsync(); } /// /// Handles the DoWork event of the backgroundWorker1 control. /// /// The source of the event. /// The instance containing the event data. private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { if (this.BackgroundWorkAction != null) { this.BackgroundWorkAction(); } Thread.Sleep(100); if (base.InvokeRequired) { base.BeginInvoke(new MethodInvoker(delegate { base.Close(); })); } else { base.Close(); } } /// /// Handles the ProgressChanged event of the backgroundWorker1 control. /// /// The source of the event. /// The instance containing the event data. private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) { SetMessage((e.UserState == null) ? "" : e.UserState.ToString(), e.ProgressPercentage); } } }