多线程与UI操作(二)

为了让程序尽快响应用户操作,在开发Windows应用程序时经常会使用到线程。对于耗时的操作如果不使用线程将会是UI界面长时间处于停滞状态,这种情况是用户非常不愿意看到的,在这种情况下我们希望使用线程来解决这个问题。
下面是一个使用多线程操作界面UI的代码:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Text;  
  7. using System.Windows.Forms;  
  8. using System.Threading;  
  9.   
  10. namespace ThreadPoolDemo  
  11. {  
  12.     public partial class ThreadForm : Form  
  13.     {  
  14.         public ThreadForm()  
  15.         {  
  16.             InitializeComponent();  
  17.         }  
  18.   
  19.         private void btnThread_Click(object sender, EventArgs e)  
  20.         {  
  21.             Thread thread = new Thread(new ThreadStart(Run));  
  22.             thread.Start();  
  23.         }  
  24.   
  25.         private void Run()  
  26.         {  
  27.             while (progressBar.Value < progressBar.Maximum)  
  28.             {  
  29.                 progressBar.PerformStep();  
  30.             }  
  31.         }  
  32.     }  
  33. }  

我们的本意是点击“启动”按钮来启动模拟一个操作,在进度条中显示操作的总体进度。不过如果我们真的点击“启动”按钮会很失望,因为它会抛出一个System.InvalidOperationException异常,异常描述就是“线程间操作无效: 从不是创建控件‘progressBar’的线程访问它。”
CheckForIllegalCrossThreadCalls属性
之所以会出现这样的情况是因为在.NET中做了限制,不允许在调试环境下使用线程访问并非它自己创建的UI控件,这么做可能是怕在多线程环境下对界面控件进行操作会出现不可预知的情况,如果开发者可以确认自己的代码操作界面不会出现问题,可以用比较简单的方法解决,那就是设置CheckForIllegalCrossThreadCalls这个静态属性,它默认是true,如果将其设为false的话,以后在多线程环境下操作界面也不会抛出异常了,我们上面的代码可以修改为:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.Threading;  
  10.   
  11. namespace ThreadPoolDemo  
  12. {  
  13.     public partial class ThreadForm : Form  
  14.     {  
  15.         public ThreadForm()  
  16.         {  
  17.             InitializeComponent();  
  18.         }  
  19.   
  20.         private void btnThread_Click(object sender, EventArgs e)  
  21.         {  
  22.             //指示是否对错误线程的调用,即是否允许在创建UI的线程之外访问线程  
  23.             CheckForIllegalCrossThreadCalls = false;  
  24.             Thread thread = new Thread(new ThreadStart(Run));  
  25.             thread.Start();  
  26.         }  
  27.   
  28.         private void Run()  
  29.         {  
  30.             while (progressBar.Value < progressBar.Maximum)  
  31.             {  
  32.                 progressBar.PerformStep();  
  33.             }  
  34.         }  
  35.     }  
  36. }  

这样再执行程序就不会抛出异常了。

不过使用上面的代码我们可能还有些犯嘀咕,毕竟是不允许直接在线程中直接操作界面的,那么我们还可以用Invoke方法。


Invoke方法来操作界面
下面是一个例子:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.Threading;  
  10.   
  11. namespace ThreadPoolDemo  
  12. {  
  13.     public partial class ThreadForm : Form  
  14.     {  
  15.         //定义delegate以便Invoke时使用  
  16.         private delegate void SetProgressBarValue(int value);  
  17.         public ThreadForm()  
  18.         {  
  19.             InitializeComponent();  
  20.         }  
  21.   
  22.         private void btnThread_Click(object sender, EventArgs e)  
  23.         {  
  24.             progressBar.Value = 0;  
  25.             //指示是否对错误线程的调用,即是否允许在创建UI的线程之外访问线程  
  26.             //CheckForIllegalCrossThreadCalls = false;  
  27.             Thread thread = new Thread(new ThreadStart(Run));  
  28.             thread.Start();  
  29.         }  
  30.         //使用线程来直接设置进度条  
  31.         private void Run()  
  32.         {  
  33.             while (progressBar.Value < progressBar.Maximum)  
  34.             {  
  35.                 progressBar.PerformStep();  
  36.             }  
  37.         }  
  38.   
  39.         private void btnInvoke_Click(object sender, EventArgs e)  
  40.         {  
  41.             progressBar.Value = 0;  
  42.             Thread thread = new Thread(new ThreadStart(RunWithInvoke));  
  43.             thread.Start();  
  44.         }  
  45.         //使用Invoke方法来设置进度条  
  46.         private void RunWithInvoke()  
  47.         {  
  48.             int value = progressBar.Value;  
  49.             while (value< progressBar.Maximum)  
  50.             {  
  51.                 //如果是跨线程调用  
  52.                 if (InvokeRequired)  
  53.                 {  
  54.                     this.Invoke(new SetProgressBarValue(SetProgressValue), value++);  
  55.                 }  
  56.                 else  
  57.                 {  
  58.                     progressBar.Value = ++value;  
  59.                 }  
  60.             }  
  61.         }  
  62.         //跟SetProgressBarValue委托相匹配的方法  
  63.         private void SetProgressValue(int value)  
  64.         {  
  65.             progressBar.Value = value;  
  66.         }  
  67.     }  
  68. }  

这个方法的功能跟上面的操作是一样的,只不过不需要设置CheckForIllegalCrossThreadCalls属性,而且还不会抛出异常。

posted on 2017-03-17 16:28  可爱的春哥  阅读(193)  评论(0编辑  收藏  举报

导航