delegatevoid SetTextCallback(string text);
// If the calling thread is different from the thread that
// created the TextBox control, this method creates a
// SetTextCallback and calls itself asynchronously using the
// Invoke method.
//
// If the calling thread is the same as the thread that created
// the TextBox control, the Text property is set directly.
privatevoid 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;
}
}
newClient.cnctChange += this.cnctChange; //可以将一个对象的委托赋给另一个委托
本文详细解释了如何使用委托和异步方法在不同的线程中更新文本控件的文本。通过检查调用线程是否与控件创建线程相同,决定直接设置文本还是使用委托和异步调用来更新文本。
1363

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



