Updating The UI From Another Task

In WPF, the “Dispatcher” is the way to call a function that belongs to a class in one thread from another thread.

As long as the class you are calling supports the Dispatcher object (all UI classes such as Window  do) , you can use the following code. Note that the function to execute is defined in the function call to  BeginInvoke()

If you want the calling thread to wait until the dispatcher function has run to completion before continuing (i.e. synchronous execution), you can use the Invoke()  method rather than BeginInvoke() .


Locking is the common way of making thread-safe code in C#. It is done with the keyword lock, which is a native keyword in C# (it doesn’t require any library or .dll to be included for it to work).

A lock can be taken out on any object, although it is common to make a separate object of type object , just for locking.


Queues are a good way of passing information between two asynchronous threads. The .NET language supports queues with the System.Collections.Generic.Queue<T>  class.

The actual objects that are queued can be defined as any standard class. To be thread-safe, reading and writing from the queue (enqueue and dequeue respectively), requires the use of a lock so you don’t happen to be half way through writing to it when you start to read from it.

Posted: April 11th, 2013 at 3:58 pm
Last Updated on: June 24th, 2013 at 3:11 pm

Leave a Reply