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;
}
}
SetText(time.ToString());
这篇博客探讨了在多线程环境下如何使用委托正确地更新窗体控件,特别是在设置文本框文本时的安全方法。通过InvokeRequired检查和回调函数,确保了UI操作在正确的线程中执行。
2305

被折叠的 条评论
为什么被折叠?



