对 Windows 窗体控件进行线程安全调用
首先查询控件的 InvokeRequired 属性。
如果 InvokeRequired 返回 true,则使用实际调用控件的某方法的委托来调用 Invoke。
如果 InvokeRequired 返回 false,则直接调用控件。
例如
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;
}
}
以上代码来自http://msdn.microsoft.com/zh-cn/library/ms171728.aspx