跨线程调用控件的实现
2014-04-26 17:30
239 查看
在VS 2005 中考虑到线程的安全性,不允许跨线程调用控件! 为了解决这一问题
1. 将Control 的 CheckForIllegalCrossThreadCalls 属性设置为假, 不去捕获错误线程的调用,但这种做法是不安全的!
2.使用委托异步调用
见代码:
www.nifdd.com
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace TabSelect
...{
class MyThread
...{
public delegate void SetLabelText(string text); //定义委托
Control control;
Thread thread;
ThreadStart start;
www.njfie.com
构造方法,初始化线程,以及要改变属性的控件#region 构造方法,初始化线程,以及要改变属性的控件
public MyThread(Control control,int num)
...{
this.control = control;
start = new ThreadStart(BuildNum2); //订阅线程要执行的代码
this.num2 = num;
thread = new Thread(start);
thread.Start();
}
#endregion
线程中需要执行的方法 通过设置Control 属性,指示是否捕获错误线程的调用#region 线程中需要执行的方法 通过设置Control 属性,指示是否捕获错误线程的调用
public void BuildNum()
1. 将Control 的 CheckForIllegalCrossThreadCalls 属性设置为假, 不去捕获错误线程的调用,但这种做法是不安全的!
2.使用委托异步调用
见代码:
www.nifdd.com
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace TabSelect
...{
class MyThread
...{
public delegate void SetLabelText(string text); //定义委托
Control control;
Thread thread;
ThreadStart start;
www.njfie.com
构造方法,初始化线程,以及要改变属性的控件#region 构造方法,初始化线程,以及要改变属性的控件
public MyThread(Control control,int num)
...{
this.control = control;
start = new ThreadStart(BuildNum2); //订阅线程要执行的代码
this.num2 = num;
thread = new Thread(start);
thread.Start();
}
#endregion
线程中需要执行的方法 通过设置Control 属性,指示是否捕获错误线程的调用#region 线程中需要执行的方法 通过设置Control 属性,指示是否捕获错误线程的调用
public void BuildNum()
相关文章推荐
- ACM-计算几何之Surround the Trees——hdu1392
- android pull解析xml文件实例
- ExtJs4 与 SASS 的亲密接触——使用 SASS 自定义主题
- CocoaPods安装和使用教程
- 读《高质量C++编程》总结
- 把Session保存在SqlServer中的实现
- ecshop 实现多域名共用一套系统
- tolua++ vs2013 lua5.1
- consistent hash
- 认识JSON+jayrock
- HDU 2516 取石子游戏
- Linux 第二步
- 步入Linux
- ARM内核全解析,从ARM7,ARM9到Cortex-A7,A8,A9,A12,A15到Cortex-A53,A57
- 几种服务器端IO模型的简单介绍及实现
- ARM内核全解析,从ARM7,ARM9到Cortex-A7,A8,A9,A12,A15到Cortex-A53,A57
- 黑马程序员_高新技术_JDK1.5的新特性
- 燕歌行
- 燕歌行
- c++-类的初始化列表