C#: Update UI Component with Threadsafe Operation

Sometimes when you try to access UI component from a different thread, you’ll run into exception on you can’t access UI component from a different Thread than the original UI thread that creates the component.

For example, I have a Label, I would like to set the Label1.txt to some text from inside of a backgroundWorker.

Here’s some reference links:
http://stackoverflow.com/questions/711408/best-way-to-invoke-any-cross-threaded-code

They come up with a very simple way of update any UI component from a different thread.

How to use:
1. Download the following code and place it inside your own project.

ISynchronizeInvokeExtensions

2. In the function where you want to update the UI component, invoke this class.

For example:

this.InvokeEx(f => f.label1.Text = "updating label ...");

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>