学习链接:
a、https://www.cnblogs.com/txw1958/archive/2012/09/05/csharp-BackgroundWorker.html
b、https://www.cnblogs.com/zhaoshujie/p/9634136.html
1、定义异步任务
public partial class Form1 : Form { private BackgroundWorker backgroundWorker = new BackgroundWorker(); static loginTip loginTip = new loginTip(); } public Form1() { backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork); backgroundWorker.WorkerSupportsCancellation = true; } //显示登陆提示 private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { loginTip.ShowDialog();//显示加载页面 }
2、调用
if (!backgroundWorker.CancellationPending) { backgroundWorker.CancelAsync(); } backgroundWorker.RunWorkerAsync();
3、关闭
this.Invoke(new Action(() => { loginTip.Hide(); })); backgroundWorker.CancelAsync();