How do I update the GUI from another thread?

The simplest way is an anonymous method passed into Label.Invoke:

// Running on the worker thread
string newText = "abc";
form.Label.Invoke((MethodInvoker)delegate {
    // Running on the UI thread
    form.Label.Text = newText;
});
// Back on the worker thread

Notice that Invoke blocks execution until it completes–this is synchronous code. The question doesn’t ask about asynchronous code, but there is lots of content on Stack Overflow about writing asynchronous code when you want to learn about it.

Leave a Comment