Saturday, May 30, 2009

Safe update of windows control from other threads

// 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;
}
}

No comments:

Post a Comment