多线程程序中线程安全的操纵可视化控件
2012-05-06 21:49
295 查看
在我们写WinForm程序的时候,如果在新创建的另外一个线程中直接操纵界面的可视化控件,会提示错误。如下提示:
线程间操作无效: 从不是创建控件“richTextBox1”的线程访问它。
代码如下所示,当没有从线程中操作可视化控件的时候,代码是安全的。
但是当另外一个按钮从其他的线程直接操作可视化控件以后,就会提示错误的操作:代码如下:
这其中的原因,在于windows窗体编程的消息机制,windows维持一个给窗体传递的消息列表,记录每个事件消息发送给了每个可视化控件,系统单独的为消息列表维护了一个线程,并且这个消息列表属于队列,先进先出。如果从自己添加的线程中给可视化控件传递消息,则会打乱系统的消息列表,所以是系统不允许的。但是我们又需要在多线程中操作可视化控件,怎么办呢?可以用委托和反射来实现。
实现的代码并不复杂,只要对每个可视化控件的操作添加一个委托就可以了。
一种实现的代码如下所示:
测试表明,程序运行正常。
上面使用了显示定义的委托,也可以通过下面这种方法来定义委托,这里使用了C# 系统已经定义好的一种委托Action<>:
同样程序运行正常,这里比较简单,所以就不给出用户界面截图了。
线程间操作无效: 从不是创建控件“richTextBox1”的线程访问它。
代码如下所示,当没有从线程中操作可视化控件的时候,代码是安全的。
namespace SafeThread { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button2_Click(object sender, EventArgs e) { this.richTextBox1.Text += "add message through main thread!\n"; } //private void button1_Click(object sender, EventArgs e) //{ // Thread thread = new Thread(new ThreadStart(addMsg)); // thread.IsBackground = true; // thread.Start(); //} //private void addMsg() //{ // this.richTextBox1.Text += "add message through other thread!\n"; //} } }
但是当另外一个按钮从其他的线程直接操作可视化控件以后,就会提示错误的操作:代码如下:
namespace SafeThread { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button2_Click(object sender, EventArgs e) { this.richTextBox1.Text += "add message through main thread!\n"; } private void button1_Click(object sender, EventArgs e) { Thread thread = new Thread(new ThreadStart(addMsg)); thread.IsBackground = true; thread.Start(); } private void addMsg() { this.richTextBox1.Text += "add message through other thread!\n"; } } }
这其中的原因,在于windows窗体编程的消息机制,windows维持一个给窗体传递的消息列表,记录每个事件消息发送给了每个可视化控件,系统单独的为消息列表维护了一个线程,并且这个消息列表属于队列,先进先出。如果从自己添加的线程中给可视化控件传递消息,则会打乱系统的消息列表,所以是系统不允许的。但是我们又需要在多线程中操作可视化控件,怎么办呢?可以用委托和反射来实现。
实现的代码并不复杂,只要对每个可视化控件的操作添加一个委托就可以了。
一种实现的代码如下所示:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Threading; namespace SafeThread { public partial class Form1 : Form { private delegate void callRichTextBox(string s); public Form1() { InitializeComponent(); } private void button2_Click(object sender, EventArgs e) { this.richTextBox1.Text += "add message through main thread!\n"; } private void button1_Click(object sender, EventArgs e) { Thread thread = new Thread(new ParameterizedThreadStart(addMsg)); thread.IsBackground = true; thread.Start("add message through other thread!"); } private void addMsg(object log) { if (richTextBox1.InvokeRequired) { callRichTextBox call = delegate(string s) { this.richTextBox1.Text += s + "\n"; }; // 下面调用的Invoke()函数,如果委托有参数,则第二个参数则为委托的参数 this.richTextBox1.Invoke(call, log); } else { this.richTextBox1.Text += "add message through other thread!\n"; } } } }
测试表明,程序运行正常。
上面使用了显示定义的委托,也可以通过下面这种方法来定义委托,这里使用了C# 系统已经定义好的一种委托Action<>:
namespace SafeThread { public partial class Form1 : Form { //private delegate void callRichTextBox(string s); public Form1() { InitializeComponent(); } private void button2_Click(object sender, EventArgs e) { this.richTextBox1.Text += "add message through main thread!\n"; } private void button1_Click(object sender, EventArgs e) { Thread thread = new Thread(new ParameterizedThreadStart(addMsg)); thread.IsBackground = true; thread.Start("add message through other thread!"); } private void addMsg(object log) { if (richTextBox1.InvokeRequired) { //callRichTextBox call = delegate(string s) { this.richTextBox1.Text += s + "\n"; }; Action<string> call = delegate(string t) { this.richTextBox1.Text += t + "\n"; }; // 下面调用的Invoke()函数,如果委托有参数,则第二个参数则为委托的参数 this.richTextBox1.Invoke(call, log); } else { this.richTextBox1.Text += "add message through other thread!\n"; } } } }
同样程序运行正常,这里比较简单,所以就不给出用户界面截图了。
相关文章推荐
- Windows 窗体控件中的多线程处理之:如何对 Windows 窗体控件进行线程安全调用
- 多线程 —— 使用锁编写线程安全的程序
- Winform中进行线程安全的多线程操作(主线程控件,非backgroundWorker)
- *****C# 程序代码动态修改Label值(对Windows窗体控件进行线程安全调用)
- 使用多线程提高 Windows 窗体应用程序的性能时,必须注意以线程安全方式调用控件
- 多线程原理、线程安全函数和多线程程序需要注意的问题
- Java多线程模拟售票程序,及线程安全问题
- c#之多线程 (跨线程访问可视化控件)
- C++/CLI 窗体程序通过空间的name操纵控件
- 数字猜大小(经典多线程和自动生成控件的小例子)和确定程序的运行时间方法
- .net2.0的线程安全下对控件的多线程控制
- 实现一个无锁的Stack,并写一段测试代码(多线程访问),证明这个Stack是线程安全的。给出程序以及运行的截图。
- delphi 一个自动控制机的硅控板检测程序,用多线程和API,没有用控件,少做改动就能用 用485开发
- Java千百问_02基本使用(012)_如何编写多线程Socket程序
- 求APS可视化排程源码、程序设计流程图、开发文档、相关资料、程序、思路。请作过ERP的朋友顶力相助(那怕是排课软件方面的也可以参考)试 。
- C#中工作线程和多线程跨线程操作控件
- GDB调试-多进程与多线程程序
- 如何提高多线程程序的cpu利用率
- 【转帖】以程序的方式操纵NTFS的文件权限
- 微信小程序基础之交互操作控件