我的代码如下void load()
{
    Thread receiveThread = new Thread(ReceiveMessage);
    receiveThread.Start();
}
private void ReceiveMessage()
{
    while (true)
            {
                try
                {
                    byte[] receiveBytes = receiveUpdClient.Receive(ref remoteIpEndPoint);                    
                    string get = Encoding.Unicode.GetString(receiveBytes);
                    if (get == "head")
                        {
                            PictureBox p = new PictureBox();
                            p.Image = Properties.Resources.head;
                            p.SizeMode = PictureBoxSizeMode.StretchImage;
                            panel1.Controls.Add(p);//这里提示标题错误
                         }
                 }
                 catch(Exception e)
                {
                    string x = e.Message;
                    break;
                }
           }
}
求问我该怎么改?谢谢!

解决方案 »

  1.   

    UI线程独占,不能直接怎么写,用invoke
      

  2.   

    不知道你说的是不是这个 How to: Make Thread-Safe Calls to Windows Forms Controlshttp://msdn.microsoft.com/en-us/library/ms171728.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1
      

  3.   

    panel1.Invoke((DummyDelegate)delegate() {
     panel1.Controls.Add(p);}); public delegate void DummyDelegate();这样试试
      

  4.   

    或者试试:
    this.Invoke((DummyDelegate)delegate() {PictureBox p = new PictureBox();
                                p.Image = Properties.Resources.head;
                                p.SizeMode = PictureBoxSizeMode.StretchImage;
                                panel1.Controls.Add(p);<span style="color: #FF0000;">
    });
    public delegate void DummyDelegate();