如果在线程中把收到的数据显示到窗体的文本框中
我的代码如下:
声明:
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中显示呢。
[解决办法]
没问题,以下为测试代码
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(); }