che*_*unz 4 c# multithreading label bluetooth
大家好我有一个能够读取蓝牙流接收数据的线程.在发件人部分,我做了一个while循环,其中count继续增加+ 1.我做了一个messagebox.show(测试); 它工作正常,但当我做label.text =测试我得到:
"必须使用Control.Invoke与在单独线程上创建的控件进行交互." 错误.我在C#中的关注代码:
线程t =新线程(新的ThreadStart(readStream)); t.Start(); public void readStream(){while(true){String test = manager.Reader.ReadLine(); label1.Text = test; }}
我的问题是,我如何更新线程中的标签?控制调用的任何简单方法?
你好这里是一个如何做到这一点的例子:
http://msdn.microsoft.com/en-us/library/ms171728.aspx
如果要从另一个线程更新标签,则应使用与此类似的功能.您无法直接更新.
简而言之:你应该写这样的东西:
delegate void SetTextCallback(string text);
private void SetText(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 (this.textBox1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.textBox1.Text = text;
}
}
| 归档时间: |
|
| 查看次数: |
3916 次 |
| 最近记录: |