子线程调用winform界面的 UI控件 的安全调用。
- 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;
}
}
本文介绍了一种在Windows Forms应用程序中从子线程安全地更新UI控件的方法。通过使用委托和Invoke方法,可以确保在不同线程间正确且安全地更新TextBox等UI元素的内容。
793

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



