第一种:使用BackgroundWorker进行进度条控制
BackgroundWorker对象有三个主要的事件:
DoWork - 当BackgroundWorker对象的多线程操作被执行时触发。
RunWokerCompleted - 当BackgroundWoker对象的多线程操作完成时触发。
ProgressChanged - 当BackgroundWorker对象的多线程操作状态改变时触发。
WorkerReportsProgress - 如果想让BackgroundWorker对象以异步的方式报告线程实时进度,必须将该属性的值设为true。
BackgroundWorker对象的ReportProgress方法用于向主线程返回后台线程执行的实时进度。
实例代码一,控制主窗体中的进度条显示
- public partial class Form1 : Form
- {
-
-
-
- private BackgroundWorker bkWorker = new BackgroundWorker();
-
-
-
-
- private int percentValue = 0;
-
- public Form1()
- {
- InitializeComponent();
-
- bkWorker.WorkerReportsProgress = true;
- bkWorker.WorkerSupportsCancellation = true;
- bkWorker.DoWork += new DoWorkEventHandler(DoWork);
- bkWorker.ProgressChanged += new ProgressChangedEventHandler(ProgessChanged);
- bkWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(CompleteWork);
- }
-
- private void btnStart_Click(object sender, EventArgs e)
- {
- percentValue = 10;
- this.progressBar1.Maximum = 1000;
-
- bkWorker.RunWorkerAsync();
- }
-
- public void DoWork(object sender, DoWorkEventArgs e)
- {
-
- e.Result = ProcessProgress(bkWorker, e);
- }
-
- public void ProgessChanged(object sender, ProgressChangedEventArgs e)
- {
-
- this.progressBar1.Value = e.ProgressPercentage;
- int percent = (int)(e.ProgressPercentage / percentValue);
- this.label1.Text = "处理进度:" + Convert.ToString(percent) + "%";
- }
-
- public void CompleteWork(object sender, RunWorkerCompletedEventArgs e)
- {
- this.label1.Text = "处理完毕!";
- }
-
- private int ProcessProgress(object sender, DoWorkEventArgs e)
- {
- for (int i = 0; i <= 1000; i++)
- {
- if (bkWorker.CancellationPending)
- {
- e.Cancel = true;
- return -1;
- }
- else
- {
-
- bkWorker.ReportProgress(i);
-
-
- System.Threading.Thread.Sleep(1);
- }
- }
-
- return -1;
- }
- }
下面是运行结果
实例代码二,控制弹出窗体中的进度条显示
主窗体代码:
- public partial class Form1 : Form
- {
- private BackgroundWorker bkWorker = new BackgroundWorker();
- private Form2 notifyForm = new Form2();
-
- public Form1()
- {
- InitializeComponent();
-
-
-
-
- CheckForIllegalCrossThreadCalls = false;
-
- bkWorker.WorkerReportsProgress = true;
- bkWorker.WorkerSupportsCancellation = true;
- bkWorker.DoWork += new DoWorkEventHandler(DoWork);
- bkWorker.ProgressChanged += new ProgressChangedEventHandler(ProgessChanged);
- bkWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(CompleteWork);
- }
-
- private void btnStart_Click(object sender, EventArgs e)
- {
- notifyForm.StartPosition = FormStartPosition.CenterParent;
-
- bkWorker.RunWorkerAsync();
- notifyForm.ShowDialog();
- }
-
- public void DoWork(object sender, DoWorkEventArgs e)
- {
-
- e.Result = ProcessProgress(bkWorker, e);
- }
-
- public void ProgessChanged(object sender, ProgressChangedEventArgs e)
- {
-
- notifyForm.SetNotifyInfo(e.ProgressPercentage, "处理进度:" + Convert.ToString(e.ProgressPercentage) + "%");
- }
-
- public void CompleteWork(object sender, RunWorkerCompletedEventArgs e)
- {
- notifyForm.Close();
- MessageBox.Show("处理完毕!");
- }
-
- private int ProcessProgress(object sender, DoWorkEventArgs e)
- {
- for (int i = 0; i <= 1000; i++)
- {
- if (bkWorker.CancellationPending)
- {
- e.Cancel = true;
- return -1;
- }
- else
- {
-
- bkWorker.ReportProgress(i / 10);
-
-
- System.Threading.Thread.Sleep(1);
- }
- }
-
- return -1;
- }
- }
子窗体代码
- public partial class Form2 : Form
- {
- public Form2()
- {
- InitializeComponent();
- }
-
- public void SetNotifyInfo(int percent, string message)
- {
- this.label1.Text = message;
- this.progressBar1.Value = percent;
- }
- }
下面是运行结果
第二种,使用Thread来实现
使用Thread实现,虽然步骤上比较麻烦,但是调用流程比较简单,也是一种可以参考的方法
使用时,首先要定义代理以及函数,然后实现线程函数,在线程函数中调用代理,最后启动线程,传入线程函数。
下面是实例代码:
- public partial class Form1 : Form
- {
- private Form2 progressForm = new Form2();
-
- private delegate void funHandle(int nValue);
- private funHandle myHandle = null;
-
- public Form1()
- {
- InitializeComponent();
- }
-
- private void btnStart_Click(object sender, EventArgs e)
- {
-
- System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(ThreadFun));
- thread.Start();
- }
-
-
-
-
- private void ShowProgressBar()
- {
- myHandle = new funHandle(progressForm.SetProgressValue);
- progressForm.ShowDialog();
- }
-
-
-
-
- private void ThreadFun()
- {
- MethodInvoker mi = new MethodInvoker(ShowProgressBar);
- this.BeginInvoke(mi);
-
- System.Threading.Thread.Sleep(1000);
-
- for (int i = 0; i < 1000; ++i)
- {
- System.Threading.Thread.Sleep(5);
-
- this.Invoke(this.myHandle, new object[] { (i / 10) });
- }
- }
- }
子窗体代码
- public partial class Form2 : Form
- {
- public Form2()
- {
- InitializeComponent();
- }
-
- public void SetProgressValue(int value)
- {
- this.progressBar1.Value = value;
- this.label1.Text = "Progress :" + value.ToString() + "%";
-
-
- if (value == this.progressBar1.Maximum - 1) this.Close();
- }
- }
下面是运行结果图
C#调用耗时函数时显示进度条浅探
原文:http://www.cnblogs.com/liushunli/p/4280165.html