zoukankan      html  css  js  c++  java
  • 黄聪:C#“多线程线程间操作无效: 从不是创建控件的线程访问它。”,跨线程修改控件属性解决方案

    解决方案就是使用代理,在代理中调用主线程的方法来控制控件

            /// <summary>
            /// 声明代理
            /// </summary>
            delegate void SetTextCallBack(string text);
    
            /// <summary>
            /// 代理函数,在线程中使用
            /// </summary>
            private void Tip(string text)
            {
                //判断主线程的控件是否需要使用代理来访问
                if (this.tbTip.InvokeRequired)
                {
                    //创建一个代理
                    SetTextCallBack stcb = new SetTextCallBack(Tip);
                    //执行代理
                    this.Invoke(stcb, new object[] { text });
                }
                else
                {
                    //调用主线程的方法,并传递参数,这样就可以在_tip方法里面调用主线程的控件并修改属性了
                    _tip(text);
                }
            }
  • 相关阅读:
    Redis
    vscode
    uget + aria2
    Nodejs 安装
    NPM
    ?Swift获取手机设备信息
    C语言的32个关键字
    MVC-Html.Label(TextBox、TextArea、RadioButton、CheckBox)
    常用正则表达式
    MVC-Razor引擎布局
  • 原文地址:https://www.cnblogs.com/huangcong/p/7851511.html
Copyright © 2011-2022 走看看