线程间操作无效: 从不是创建控件的线程访问它

 解决方法一:

updateScollText = new System.Threading.Thread(new System.Threading.ThreadStart(ScrollText));

  //加这句

          Control.CheckForIllegalCrossThreadCalls = false;
            updateScollText.Start();

 

解决方法二:

//先做如下函数声明

 // This delegate enables asynchronous calls for setting
        // the text property on a TextBox control.
        delegate void SetTextCallback(TextBox textBoxTemp, string text);
        private void SetText(TextBox textBoxTemp, string text)
        {
            // InvokeRequired required compares the thread ID of the
            // calling thread to the thread ID of the creating thread.
            // If these threads are different, it returns true.
            if (textBoxTemp.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                this.Invoke(d, new object[] { textBoxTemp, text });
            }
            else
            {
                textBoxTemp.Text = text;
            }
        }

//在进程中显示信息时,使用如下命令:

  SetText(tb_ServerExceptionCount, m_socketServer.ServerExceptionCount.ToString());

 

解决方法三:

使用backgroundWorker控件

 

 

你可能感兴趣的:(thread,String,object,asynchronous,textbox)