//定义
BackgroundWorker Bworker = new BackgroundWorker();
//Bworker.WorkerReportsProgress = true;
Bworker.DoWork += new DoWorkEventHandler(worker_DoWork);
//Bworker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
Bworker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
Bworker.WorkerSupportsCancellation = true; //在执行过程中允许用户取消当前的操作
Bworker.RunWorkerAsync();
private void trigger(){
//在触发事件中取消
Bworker.CancelAsync();
}
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
for(;;){
//在操作的过程中需要检查用户是否取消了当前的操作。
if (bgWorker.CancellationPending == true)
{
e.Cancel = true;
break;
}
}
}