Skip to content

Instantly share code, notes, and snippets.

@Fresh-Dev
Created February 10, 2016 19:23
Show Gist options
  • Save Fresh-Dev/030c614a8b0ab7aac74c to your computer and use it in GitHub Desktop.
Save Fresh-Dev/030c614a8b0ab7aac74c to your computer and use it in GitHub Desktop.
Threadsafe-GUI Interaction
// 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