Skip to content

Instantly share code, notes, and snippets.

@dkalamari
Created May 20, 2013 12:57
Show Gist options
  • Save dkalamari/5612067 to your computer and use it in GitHub Desktop.
Save dkalamari/5612067 to your computer and use it in GitHub Desktop.
Update UI control from 2. thread
//Control.Invoke: Executes on the UI thread, but calling thread waits for completion before continuing.
//Control.BeginInvoke: Executes on the UI thread, and calling thread doesn't wait for completion.
//Normally every BeginXXX should have a corresponding EndXXX call, usually in the callback.
public void updateGuiFromWorkerThread(String message, Color color)
{
Invoke(new StatusEventHandler(UpdateUI), new StatusArguments(message, color));
}
private void UpdateUI(StatusArguments e)
{
lblStatus.Text = "Status: " + e.message;
lblStatus.BackColor = e.color;
}
private delegate void StatusEventHandler(StatusArguments e);
private class StatusArguments : System.EventArgs
{
public String message;
public Color color;
public StatusArguments(String message, Color color)
{
this.message = message;
this.color = color;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment