zoukankan      html  css  js  c++  java
  • winform跨线程访问控件

    首先说下,.net 2.0以后加强了安全机制,不允许在winform中直接跨线程访问控件的属性。所以除了控件所在的线程外的线程调用会抛异常

    (Cross-thread operation not valid:Control 'textBox1' accessed from a thread other than the thread it was created on .)

    下面进入正题:

      第一种方法:

        

    1
    2
    3
    4
    5
    6
    public DomainQuery2()
           {
     
               InitializeComponent();
               System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;//设置该属性 为false
           }

      这种方法很方便,但是会有安全问题。

     

      第二种方法:

        

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    private delegate void FlushClient(string a); //代理委托
    //代理赋值方法
    private void TextboxFZ3(string str)
            {
                if (this.textBox3.InvokeRequired)
                {
                    FlushClient fc = new FlushClient(TextboxFZ3);
                    this.Invoke(fc, str); //通过代理调用刷新方法
                }
                else
                {
                    this.textBox3.AppendText(str + " ");
                }
            }
    //这个就是要开启线程的方法  如果要操作控件,必须调用上面的方法
    private void FZ(object obj)  
    {
          TextboxFZ3(obj);
    }
    //点击执行
    private void button1_Click(object sender, EventArgs e)
    {
         Thread thread = new Thread(new ParameterizedThreadStart(SerchInfo));
                thread.IsBackground = true;
                thread.Start(“我是子线程操作的控件赋值”);
    }

        我喜欢先上代码然后解释,第一句:代理委托,当要操作控件的时候做一个判断,如果是子线程要操作,就使用代理委托操作(这里不理解就在赋值方法那里打个断点调试),FZ是具体操作的逻辑。就是这么简单

     

    这两种方法我比较赞同第二种,虽然比第一种方法的代码多,但是相对来说安全性也是有的。

  • 相关阅读:
    forceStopPackage应用中关闭其他应用程序
    Android为什么选择binder
    Ril分析一——rild进程
    Ril分析五——ril学习总结
    Binder基本概念流程学习
    Ril分析四——来自网络端事件流程
    inno setup详细使用教程
    如何下载mysql 5.5.32 二进制包
    eclipse搭建springboot开发环境
    “STO”是“Store”的缩写,意思是“商场”
  • 原文地址:https://www.cnblogs.com/asdyzh/p/9876863.html
Copyright © 2011-2022 走看看