How to update the GUI from a different thread

HomeC#Featured

How to update the GUI from a different thread

Exploring LINQ to Objects: Powerful Data Manipulation in C#
Create a case insensitive “Contains(string)”
Calling a C function from C#

When using a multithreaded application, the GUI will run in the initial thread created by application when it’s started, any additional thread created at runtime will throw an error if it tries to update the GUI.

To be able to update the GUI the simplest way is anonymous method passed to the object we’re trying to update in the GUI


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

Invoke will block execution until it completes what is doing because it’s synchronous code

COMMENTS

DISQUS: 0