/// <summary> /// 后台异步运行代码 /// </summary> /// <param name="doWork">需要异步执行方法委托</param> /// <param name="runWorkerCompleted">异步执行完成后的方法委托</param> public static void RunInBackground(DoWorkEventHandler doWork, RunWorkerCompletedEventHandler runWorkerCompleted) { var loadingFrm = new LoadingFrm(); using (var worker = new BackgroundWorker()) { worker.DoWork += doWork; worker.RunWorkerCompleted += delegate(object obj, RunWorkerCompletedEventArgs env) { loadingFrm.Close();//关闭等待窗口 }; worker.RunWorkerCompleted += runWorkerCompleted; worker.RunWorkerAsync(); //执行异步线程 loadingFrm.ShowDialog(); //打开等待窗口 } }
public void Stop() { if (thread == null) { return; } if (frm.InvokeRequired) { frm.Invoke(new StopDelegate(Stop)); return; } frm.Close(); thread = null; }