BackgroundWorker worker = new BackgroundWorker();//定义后台进程
worker.WorkerReportsProgress = true;//允许报告进度
worker.WorkerSupportsCancellation = true;//允许中途取消
worker.DoWork += new DoWorkEventHandler(DoWork);//注册后台工作事件
worker.ProgressChanged += new ProgressChangedEventHandler(OnProgressChanged);//注册进度报告事件
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(OnProcessCompleted);//注册工作完成事件
if(!worker.IsBusy)
{
worker.RunWorkerAsync();//开始异步工作
} #region 配置后台进程事件 private void DoWork(object sender, DoWorkEventArgs e)
{//定义后台工作 }
private void OnProgressChanged(object sender, ProgressChangedEventArgs e)
{//定义进度报告事件 }
//工作完成后执行的事件
private void OnProcessCompleted(object sender, RunWorkerCompletedEventArgs e)
{//定义工作完成事件 } #endregion