安全更新的Windows控件从其他线程
转自:http://dev.mjxy.cn/a-Security-Update-Windows-controls-from-another-thread.aspx
// Safe update of windows control from other threads
delegate void UpdateReportCallback(string text);
private void UpdateReport(string message)
{
// 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.textBoxReport.InvokeRequired)
{
UpdateReportCallback d = new UpdateReportCallback(UpdateReport);
this.Invoke(d, new object[] { message });
}
else
{
textBoxReport.Text = message + System.Environment.NewLine + textBoxReport.Text;
}
}