我的代码如下:
声明:
delegate void SetTextCallback(string text);
在线程中调用
textBox1SetText(clientcommand);为string 数据
代码:
private void textBox1SetText(string str)        {            if (this.rtb_note.InvokeRequired)
            {
                //WriteTxt(text);
                SetTextCallback d = new SetTextCallback(textBox1SetText);
                this.Invoke(d, new object[] { str });
            }
            else
            {
                this.rtb_note.Text = this.rtb_note.Text + "\r\n" + str;
            }
        }
为什么不能把信息加到rtb_note中显示呢。

解决方案 »

  1.   

    没问题,以下为测试代码        delegate void SetTextCallback(string text);
            private void textBox1SetText(string str)
            {            if (this.textBox3.InvokeRequired)
                {                SetTextCallback d = new SetTextCallback(textBox1SetText);
                    this.Invoke(d, new object[] { str });
                }
                else
                {
                    this.textBox3.Text = this.textBox3.Text + "\r\n" + str;
                }
            }
            private void Foo()
            {
                textBox1SetText("abc");
            }
            private void button10_Click(object sender, EventArgs e)
            {
                Thread t = new Thread(new ThreadStart(Foo));
                t.Start();
            }
      

  2.   

    没出错? clientcommand是string?
      

  3.   

    private void ServiceClient()
            {
                //Socket client = null;       
                    Byte[] buffer = new Byte[10];
                    byte[] sbuffer = new byte[20];
                    //if (clientsocket.Connected)
                
                        textBox1SetText("已连接");
                        clientsocket.Receive(buffer);
                        //string clientcommand = System.Text..Encoding.ASCII.GetString(buffer);
                        string clientcommand = System.Text.UnicodeEncoding.ASCII.GetString(buffer);
                        textBox1SetText(clientcommand);
                        sbuffer = System.Text.UnicodeEncoding.ASCII.GetBytes("asdf");
                        clientsocket.Send(sbuffer);
            }这个是我全部调用的代码。 如果把textBox1SetText屏蔽了,就能正常返回数据。如果这个不屏蔽,那软件一直处于等待状态 ,跟踪的时候,就是到textBox1SetText 时。处于等待
      

  4.   

    调用 ServiceClient 的地方代码是怎样的