第一种:使用BackgroundWorker进行进度条控制
BackgroundWorker对象有三个主要的事件:
DoWork - 当BackgroundWorker对象的多线程操作被执行时触发。
RunWokerCompleted - 当BackgroundWoker对象的多线程操作完成时触发。
ProgressChanged - 当BackgroundWorker对象的多线程操作状态改变时触发。
WorkerReportsProgress - 如果想让BackgroundWorker对象以异步的方式报告线程实时进度,必须将该属性的值设为true。
BackgroundWorker对象的ReportProgress方法用于向主线程返回后台线程执行的实时进度。
实例代码一,控制主窗体中的进度条显示
- public partial class Form1 : Form
- {
- /// <summary>
- /// 后台线程
- /// </summary>
- private BackgroundWorker bkWorker = new BackgroundWorker();
- /// <summary>
- /// 步进值
- /// </summary>
- 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)
- {
- // bkWorker.ReportProgress 会调用到这里,此处可以进行自定义报告方式
- 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);
- // 等待,用于UI刷新界面,很重要
- 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();
- // 使用BackgroundWorker时不能在工作线程中访问UI线程部分,
- // 即你不能在BackgroundWorker的事件和方法中操作UI,否则会抛跨线程操作无效的异常
- // 添加下列语句可以避免异常。
- 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)
- {
- // bkWorker.ReportProgress 会调用到这里,此处可以进行自定义报告方式
- 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);
- // 等待,用于UI刷新界面,很重要
- 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();
- // 代理定义,可以在Invoke时传入相应的参数
- 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();
- }
- /// <summary>
- /// 线程函数中调用的函数
- /// </summary>
- private void ShowProgressBar()
- {
- myHandle = new funHandle(progressForm.SetProgressValue);
- progressForm.ShowDialog();
- }
- /// <summary>
- /// 线程函数,用于处理调用
- /// </summary>
- private void ThreadFun()
- {
- MethodInvoker mi = new MethodInvoker(ShowProgressBar);
- this.BeginInvoke(mi);
- System.Threading.Thread.Sleep(1000); // sleep to show window
- 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();
- }
- }
下面是运行结果图