Crossthread operation not valid: accessed from a thread other than the thread it was created on.

在多线程操作界面控件或者组件的时候,经常会遇到一些问题。

我们在多线程操作控件的时候,一般都使用如下代码来操作。

delegate void MsgDel(string str);
protected void MsgEvent(string str)
{
          if (txtContent.InvokeRequired)
            {
                txtContent.BeginInvoke(new MsgDel(MsgEvent), str);
                return;
            }
        txtContent.Text=str;
}

但是Debug运行时,我仍然会遇到像如下问题“Cross-thread operation not valid: accessed from a thread other than the thread it was created on.”,不知道为什么,期待高手能够给予指点。

为了暂时解决这个问题,我暂时采用在主窗体添加下面代码的方式临时解决。

Control.CheckForIllegalCrossThreadCalls = false;  

原文地址:https://www.cnblogs.com/zwffff/p/2228869.html