private void button1_Click(object sender, EventArgs e) { backgroundWorker1.RunWorkerAsync(); } private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { for (int i = 0; i < 10; i++) { Thread.Sleep(500); backgroundWorker1.ReportProgress(i); } } private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) { Debug.WriteLine($"{e.ProgressPercentage}"); } private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { Debug.WriteLine($"over!"); }
一定要设置
backgroundWorker1.WorkerReportsProgress = true;
通常,用来显示进度条