BackgroundWorker的应用
BackgroundWorker 可以用于对控制进度条的进度,先new一个
BackgroundWorker bWork = new BackgroundWorker();
bWork.WorkerSupportsCancellation = true;//设置能够取消
bWork.WorkerReportsProgress = true;//是否报告进度
接下来就是2个事件,dowork 执行操作,
//接受进展,设置进度条
void bWork_ProgressChanged(object sender, ProgressChangedEventArgs e) { this.progressBar1.Value = e.ProgressPercentage; } void bWork_DoWork(object sender, DoWorkEventArgs e) { int i = 0; while (i <= 100) { if (bWork.CancellationPending) { e.Cancel = true; break; } bWork.ReportProgress(i++);//报告进展 Thread.Sleep(100); } }