定义了一个代理消息,在消息响应函数中listBox1和label1都无法更改属性,而MessageBox.Show却可以正常显示信息!
void glisting_GetMessage(GList.MessageType type, object Data)
        {
            //throw new Exception("The method or operation is not implemented.");
            switch(type){
                case GList.MessageType.Connect:
                    this.label1.Text  =Data.ToString();
                    break;
                case GList.MessageType.Close:
                    this.label1.Text = Data.ToString();
                     break;
                case GList.MessageType.Error:
                    MessageBox.Show( Data.ToString());
                     break;
                case GList.MessageType.GInit:
                    this.label1.Text = Data.ToString();
                     break;
                case GList.MessageType.Message:
                    this.listBox1.Items.Add(Data.ToString());
                    break;
            }
        }运行时可以捕捉到以下错误:
  Cross-thread operation net valid:Control 'listBox1' assessed from a thread other than the the thread it was created on
这是怎么回事呢,如何解决呢?

解决方案 »

  1.   

    无法在线程中直接操纵UI控件,需要用到Invoke或者beginInvoke来完成,参看
    http://blog.csdn.net/Knight94/archive/2006/08/24/1111267.aspx
      

  2.   

    非UI线程操作UI线程。可以用Invoke方法。
    如:
    public delegate DelUpdateListBox(string text);public void UpdateListBox(string text)
    {
        if(!this.InvokeRequired)
        {
             this.listBox1.Items.Add(Data.ToString());
         }
         else
         {
             DelUpdateListBox del = new DelUpdateListBox(this.UpdateListBox);
             this.Invoke(del, new object[]{text});
         }
    }
      

  3.   

    http://www.microsoft.com/china/MSDN/library/enterprisedevelopment/softwaredev/misMultithreading.mspx?mfr=true