如何用backgroudwork制作progressbar呢?
主窗体Form1.cs
子窗体frmPross.cs (添加了一个progressbar1)
【frmPross.cs】
public void OnProcessCompleted(object sender, EventArgs e){ this.Close();}public void OnProgressChanged(object sender, ProgressChangedEventArgs e){ progressBar1.Value = e.ProgressPercentage;}
public partial class Form1 : Form{ private BackgroundWorker worker = null; private void radioButton2_CheckedChanged(object sender, EventArgs e) { if (radioButton2.Checked) { frmPross pross = new frmPross(); pross.Show(); worker = new BackgroundWorker(); worker.WorkerReportsProgress = true; worker.WorkerSupportsCancellation = true; worker.DoWork += new DoWorkEventHandler(rb2_checked); worker.ProgressChanged += new ProgressChangedEventHandler(pross.OnProgressChanged); worker.ProgressChanged += new ProgressChangedEventHandler(。。。) worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(pross.OnProcessCompleted); worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(。。。); worker.RunWorkerAsync(。。。) } else { //初始化表格样式,清除单元格数值 } } private void rb2_checked(object sender, EventArgs e) { //单元格样式、大量单元格数值计算 }
private void moveButton_Click(object sender, EventArgs e) { //Show the progress bar ProgressForm progressForm = new ProgressForm(); progressForm.Show(); worker= new BackgroundWorker(); // Specify that the background worker provides progress notifications worker.WorkerReportsProgress = true; // Specify that the background worker supports cancellation worker.WorkerSupportsCancellation = true; // The DoWork event handler is the main work function of the background thread worker.DoWork += new DoWorkEventHandler(worker_DoWork); // Specify the function to use to handle progress worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged); worker.ProgressChanged += new ProgressChangedEventHandler(progressForm.OnProgressChanged); // Specify the function to run when the background worker finishes // There are three conditions possible that should be handled in this function: // 1. The work completed successfully // 2. The work aborted with errors // 3. The user cancelled the process worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted); worker.RunWorkerCompleted+=new RunWorkerCompletedEventHandler(progressForm.OnProcessCompleted); //If your background operation requires a parameter, //call System.ComponentModel.BackgroundWorker.RunWorkerAsync //with your parameter. Inside the System.ComponentModel.BackgroundWorker.DoWork //event handler, you can extract the parameter from the //System.ComponentModel.DoWorkEventArgs.Argument property. worker.RunWorkerAsync(leftList); } private void worker_DoWork(object sender, DoWorkEventArgs e) { MoveList((BackgroundWorker)sender,e); } private void MoveList(BackgroundWorker worker,DoWorkEventArgs e) { IList<string> list = e.Argument as IList<string>; for (int i = 0; i < list.Count; i++) { // Check for cancellation if (worker.CancellationPending) { e.Cancel = true; break; } else { // This will be handled in the correct thread thanks to the // internals of BackgroundWroker and AsyncOperation worker.ReportProgress((i + 1) * (100 / list.Count), list[i]); // Simulate some time consuming proccess. Thread.Sleep(500); } } } private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e) { //Add string to the right listBox rightList.Add(e.UserState as string); } private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { if (e.Cancelled) { label.Text = "Cancelled"; } else if (e.Error != null) { label.Text = "Error"; } else { label.Text = "Success!"; leftList.Clear(); } }