DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Safe Update Of Windows Control From Other Threads

04.24.2007
| 3578 views |
  • submit to reddit
        
        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;
            }
        }