您的位置:首页 > 其它

跨线程调用控件的实现

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()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: