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); } }