Skip to content

Instantly share code, notes, and snippets.

@thbar
Created March 19, 2009 08:45
Show Gist options
  • Save thbar/81652 to your computer and use it in GitHub Desktop.
Save thbar/81652 to your computer and use it in GitHub Desktop.
/* the helper, built as a Control extension method */
public delegate void Action();
public static void InUIThread(this Control control, Action action) {
if (control.InvokeRequired)
control.EndInvoke(control.BeginInvoke(action));
else
action();
}
/* some real use from a form */
class MyForm {
void SomeMethodRunningInABackgroundThread() {
/* some processing ... */
this.InUIThread(() => { statusBar.Text = newStatus; }
/* more processing ... */
this.InUIThread(() => { listBox.Items.Add(message); }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment