自学内容网 自学内容网

winform 窗体程序报错::System . Invalid Operation Exception:“线程间操作无效: 从不是创建控件的线程访问它

原文链接:https://blog.csdn.net/weixin_43848954/article/details/110386665

方法之:
取消

1.加载时取消跨线程检查

  public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
}

2.使用委托

private void button1_Click(object sender, EventArgs e)
        {
            new Action(show).BeginInvoke(null, null);
        }

        void show()
        {
            //异步外的方法。这样窗体不会假死
            while (true)
            {
                Thread.Sleep(2000);
                Action ac = new Action(showText);
                this.Invoke(ac); //在同步方法里面实现更新窗体上的数据
            }
        }

        /// <summary>
        /// 更新数据
        /// </summary>
        void showText()
        {
            richTextBox1.AppendText("更新\n");
        }


原文地址:https://blog.csdn.net/weixin_45023328/article/details/132059044

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!