我在一个线程内设置textbox.text
出错InvalidOperationException was unhandled
Illegal cross-thread operation:
Control 'textBox1' accessed from a thread other than the threa it wa created on

解决方案 »

  1.   

    可以看看.net安装下面的sample里的关于 progressbar 线程操作控件的例子, 那里解释的比较清楚了
      

  2.   

    不能访问控件,会出错的。你可以在线程方法里。用主窗体回调的方式。让窗体与控件打交道。
    如:
    fnText()
    {
      textBox1   //在这里处理
    }fn线程方法()
    {
     this.Invoke(fnText)
    }thread th = new Thread(new ThreadStart(fn线程方法));th.start()以上就可以了。
      

  3.   

    可以访问,但要通过Invoke调用。只有此方法。
      

  4.   

    public void WorkerThread()
    {
            if (form.InvokeRequired)
                  form.Invoke(new MethodInvoker(WorkerThread));
            else
            {
                  //do sth
            }
    }
    请注意WorkerThread里有个技巧, if (form.InvokeRequired) 即如果当前线程不是创建该form的线程,则将方法通通过过Invoke方法放到UI线程里去执行。