首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

怎么用backgroudwork制作progressbar呢

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

如何用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, 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();            }        } 



[解决办法]
BackgroundWorker组件的方法:

CancelAsync:请求取消挂起的后台操作
RunWorkerAsync:开始执行后台操作
ProgeressChanged:调用
Doworker:调用RunWorkerAsync方法时发生

热点排行