日期:2014-05-17  浏览次数:20951 次

如何用backgroudwork制作progressbar呢?
主窗体Form1.cs
子窗体frmPross.cs (添加了一个progressbar1)

【frmPross.cs】
C# code

public void OnProcessCompleted(object sender, EventArgs e)
{
     this.Close();
}

public void OnProgressChanged(object sender, ProgressChangedEventArgs e)
{
      progressBar1.Value = e.ProgressPercentage;
}



【Form1.cs】根据最后的代码写的,能力太差,无法举一反三,不知该如何写
C# code

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)
     {
          //单元格样式、大量单元格数值计算
     }






【根据http://www.cnblogs.com/idior/archive/2005/08/18/217796.html这篇帖子】
C# code

        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, DoWorkEventAr