`

多线程:C#.NET中使用BackgroundWorker在模态对话框中显示进度条

    博客分类:
  • C#
 
阅读更多

我们使用C#.NET编写WinForm程序时,有时候为了实现在模态对话框中实时显示后台操作的进度,这个时候需要借助于多线程操作在子窗体中显示进度条状态,在父窗体中进行后台操作。你可以在Thread类中自己创建两个线程以完成这个操作,不过C#.NET提供了BackgroundWorker对象可以帮助我们非常方便地来实现这个过程。有关Backgroundworker对象的时候我在“C#遍历文件读取Word内容以及实用BackgroundWoker对象打造平滑进度条”一文中有过介绍,大家可以去看看。

  这里是一个示例,其中展示了如何使用Backgroundworker对象在模态对话框中显示后台操作的实时进度条。

  首先是主窗体代码:

复制代码
1usingSystem;
2usingSystem.Collections.Generic;
3usingSystem.ComponentModel;
4usingSystem.Data;
5usingSystem.Drawing;
6usingSystem.Linq;
7usingSystem.Text;
8usingSystem.Windows.Forms;
9usingSystem.Threading;
10
11namespaceModalProgressDialog
12{
13publicpartialclassForm1:Form
14{
15protectedBackgroundWorkerworker=newBackgroundWorker();
16protectedForm2frm=newForm2();
17
18publicForm1()
19{
20worker.DoWork+=newDoWorkEventHandler(worker_DoWork);
21worker.ProgressChanged+=newProgressChangedEventHandler(worker_ProgressChanged);
22worker.RunWorkerCompleted+=newRunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
23
24InitializeComponent();
25}
26
27privatevoidbutton1_Click(objectsender,EventArgse)
28{
29worker.WorkerReportsProgress=true;
30worker.RunWorkerAsync();
31frm.ShowDialog();
32}
33
34voidworker_RunWorkerCompleted(objectsender,RunWorkerCompletedEventArgse)
35{
36frm.Close();
37MessageBox.Show("Done");
38}
39
40voidworker_ProgressChanged(objectsender,ProgressChangedEventArgse)
41{
42frm.ProgressValue=e.ProgressPercentage;
43}
44
45voidworker_DoWork(objectsender,DoWorkEventArgse)
46{
47CountTheTime();
48}
49
50privatevoidCountTheTime()
51{
52intinitialValue=100;
53for(intcount=0;count<initialValue;count=count+2)
54{
55Thread.Sleep(1000);
56worker.ReportProgress(count);
57}
58}
59}
60}
复制代码

 

  主窗体中只有一个按钮,当被点击时,会由BackgroundWorker对象以异步的方式去执行一个假象的后台操作CountTheTime方法。CountTheTime方法从0到100以步长为2每隔1秒更新一下进度条状态,因此这个假象的后台操作大约会持续50秒左右的时间。当程序执行时,进度条指示窗口以模态对话框的形式被弹出,然后实时显示后台操作的进度。

  BackgroundWorker对象有三个主要的事件:

  DoWork - 当BackgroundWorker对象的多线程操作被执行时触发。

  RunWokerCompleted - 当BackgroundWoker对象的多线程操作完成时触发。

  ProgressChanged - 当BackgroundWorker对象的多线程操作状态改变时触发。

  另外还有一个非常重要的属性WorkerReportsProgress - 如果想让BackgroundWorker对象以异步的方式报告线程实时进度,必须将该属性的值设为true。

  BackgroundWorker对象的ReportProgress方法用于向主线程返回后台线程执行的实时进度。

  下面是子窗体的代码:

复制代码
1usingSystem;
2usingSystem.Collections.Generic;
3usingSystem.ComponentModel;
4usingSystem.Data;
5usingSystem.Drawing;
6usingSystem.Linq;
7usingSystem.Text;
8usingSystem.Windows.Forms;
9
10namespaceModalProgressDialog
11{
12publicpartialclassForm2:Form
13{
14publicintProgressValue
15{
16get{returnthis.progressBar1.Value;}
17set{progressBar1.Value=value;}
18}
19
20publicForm2()
21{
22InitializeComponent();
23}
24}
25}
复制代码

  子窗体中放置了一个ProgressBar控件,对外可以通过ProgressValue属性来获取和修改进度条的当前值。同时,我们可以将子窗体的FormBorderStyle属性设为FixedDialog以使其看起来更像对话框,然后将MaximizeBoxMinimizeBox都设为false,将ControlBox属性设为false以隐藏窗体关闭按钮。在父窗体中,我们通过BackgroundWorker对象的RunWorkerAsync方法触发DoWork事件,此时CountTheTime()方法被执行。在CountTheTime()方法中,通过ReportProgress()方法从后台进程(父窗体)传递进度指示到主UI线程(子窗体)中,这样同时会触发ProgressChanged事件,然后我们在该事件中更新子窗体的进度条状态。下面是程序执行时的截图。

 

 

 

  注意,使用BackgroundWorker时不能在工作线程中访问UI线程部分,即你不能在BackgroundWorker的事件和方法中操作UI,否则会抛跨线程操作无效的异常。常用的方法是在主窗体的构造函数中添加CheckForIllegalCrossThreadCalls = false;语句。或者使用Thread类创建一个单独的线程,然后使用Invoke方法。可以参考下面这些内容:

http://www.cnblogs.com/chorrysky/archive/2007/02/10/646891.html

http://blog.csdn.net/marklr/archive/2009/07/10/4338518.aspx

http://www.cnblogs.com/kingsky/archive/2009/02/18/1353322.html

 

 

原文:http://www.cnblogs.com/jaxu/archive/2011/05/13/2045702.html

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics