Dispatcher to Thread relationships in WPF Dispatcher to Thread relationships in WPF wpf wpf

Dispatcher to Thread relationships in WPF


WPF application has 2 threads (one for input, the other for UI)

This statement is not entirely correct. A WPF application has only one UI thread that handles all the UI interaction and user input. There is also a "hidden" thread responsible for rendering, but normally developers don't deal with it.

Dispatcher / Thread relationship is one to one, i.e. one Dispatcher is always assoticated with one thread and can be used to dispatch execution to that thread. Dispatcher.CurrentDispatcher returns the dispatcher for the current thread, that is, when you call Dispatcher.CurrentDispatcher on a worker thread you get a dispatcher for that working thread.

Dispatchers are created on demand, which means if you access Dispatcher.CurrentDispatcher and there is no dispatcher associated with the current thread, one will be created.

That being said, the number of dispatchers in the application is always less or equal to the number of threads in the application.


WPF application by default has only one Dispatcher. The dispatcher is the only thread that will allow you to interact with UI elements. It abstracts implementations from you, so you only need to worry about being on the UI thread ie the Dispatcher.

If you are trying to directly interact with a visual (eg, set a text on a text box using txtBkx.Text = "new"), from a worker thread, then you will have to switch to a UI thread:

Application.Current.Dispatcher.Invoke(    () => { txtBkx.Text = "new"; });

Alternatively you can use SynchronizationContext.Current (while on a UI thread) and use that to execute delegates on a UI thread from a different thread. As you should note that Dispatcher.CurrentDispatcher may not always be set.

Now you can in fact create different WPF windows in the same application and have an individual dispatcher for each window:

Thread thread = new Thread(() =>{    Window1 w = new Window1();    w.Show();    w.Closed += (sender2, e2) =>                w.Dispatcher.InvokeShutdown();    System.Windows.Threading.Dispatcher.Run();});thread.SetApartmentState(ApartmentState.STA);thread.Start();  

As a side note remember in MVVM, you can update model from a non UI thread and raise property changed events from a non UI thread, as WPF will marshal PropertyChanged events for you. Raising CollectionChanged has to be on a UI thread though.


A dispatcher is always associated with a thread and a thread can have at most one dispatcher running at the same time. A thread does not need to have a dispatcher.

By default there is only one Dispatcher - For the UI. Sometimes it makes sense to have other dispatchers, other time it does not. A dispatching thread needs to block in the Dispatcher.Run() method in order to process invokes to the dispatcher. A thread such as your console input thread will not be availible to process invokes.