标签:style blog http color os 使用 io for ar
BackgroundWorker bw = new BackgroundWorker();//后台进程
bw.DoWork += new DoWorkEventHandler(bw_DoWork);//bw_DoWork这里将执行一些耗时操作
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);//操作完成后执行的事件
bw.RunWorkerAsync();//可以带参数,参数可选
void bw_DoWork(object sender, DoWorkEventArgs e)
{耗时操作,e是可选的事件参数}
BackgroundWorker backgroundWorker1 = new BackgroundWorker(); backgroundWorker1.WorkerReportsProgress = true; private void btnStart_Click(object sender, EventArgs e) { backgroundWorker1.RunWokerAsync(); } private void backgroundWorker1_DoWork(object sender, DoWorkeventArgs e) { for (int i = 1; i < 11; i++) { Thread.Sleep(2000); backgroundWorker1.ReportProgress(i*10); } } private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) { //progressBar1是ProgressBar控件 progressBar1.Value = e.ProgressPercentage; }
为了使 BackgroundWorker 可以取消后台正在执行的操作,首先要把属性WorkerSupportsCancellation 的值设置为 true。接着调用CancelAsync()方法,该方法使得属性CancellationPending 为true,利用CancellationPending 属性,可以判断是否取消后台异步操作。
BackgroundWorker backgroundWorker1 = new BackgroundWorker(); backgroundWorker1.WorkerReportsProgress = true; backgroundWorker1.WorkerSupportsCancellation = true;
private void btnCancel_Click(object sender, EventArgs e) { backgroundWorker1.CancelAsync(); }
if(backgroundWorker1.CancellationPending) { e.Cancel = true; return; }
标签:style blog http color os 使用 io for ar
原文地址:http://www.cnblogs.com/fuchongjundream/p/3937181.html