Created
February 10, 2016 19:23
-
-
Save Fresh-Dev/030c614a8b0ab7aac74c to your computer and use it in GitHub Desktop.
Threadsafe-GUI Interaction
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This method demonstrates a pattern for making thread-safe | |
// calls on a Windows Forms control. | |
// | |
// 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. | |
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment