private void OnStartClick(object sender, EventArgs e) { progressBar.Value = 0; lblProgress.Visible = true; btnStart.Enabled = false; btnCancel.Visible = true; cbUseBackgroundWorker.Enabled = false; if (cbUseBackgroundWorker.Checked) { backgroundWorker.RunWorkerAsync((int)udIterations.Value); } else { PiCalculator piCalc = new PiCalculator(); for (int i = 0; i < 100; i++) { piCalc.Run((int)udIterations.Value / 100); ReportProgress(piCalc.PI, i); } ReportProgress(piCalc.PI, 100); WorkCompleted(); } }
private void OnDoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = (BackgroundWorker)sender; worker.ReportProgress(0); PiCalculator piCalc = new PiCalculator(); int numIterations = (int)e.Argument; for (int i = 0; i < 100; i++) { piCalc.Run(numIterations / 100); worker.ReportProgress(i, piCalc.PI); if (worker.CancellationPending) { break; } } worker.ReportProgress(100, piCalc.PI); }