25

It is not entirely clear to me how many Dispatchers there are in an application and how they are related to, or referenced from Threads.

As I understand it, a WPF application has 2 threads (one for input, the other for UI) and 1 dispatcher (associated to the UI-Thread). What if I create another thread - let's call it "worker thread" - when I call Dispatcher.CurrentDispatcher on the worker thread, which dispatcher will i get?

Another case: Assume a console application with 2 threads - the main thread, and an input-thread. On the main thread, I first create the input-thread and then i call Application.Run()

Thread thread = new Thread(new ThreadStart(UserInputThreadFunction));
thread.Start();
Application.Run();

There will be one dispatcher, right? On the input-thread, does Dispatcher.CurrentDispatcher return the dispatcher of the main thread? Or what is the proper way of getting an instance to the main thread's dispatcher?

Could it be, that there are more than one dispatcher in a WPF application? Is there any case, it would make sense to create another dispatcher?

j00hi
  • 5,420
  • 3
  • 45
  • 82

3 Answers3

32

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.

Pavlo Glazkov
  • 20,498
  • 3
  • 58
  • 71
  • So, is there a run-loop running for each thread? I can hardly imagine that, due to performance reasons. – j00hi Feb 16 '11 at 11:22
  • Dispatchers are created on demand, i.e. when you call Dispatcher.CurrentDispatcher the dispatcher is created if it doesn't exist yet. – Pavlo Glazkov Feb 16 '11 at 11:45
  • So, when I call Dispatcher.CurrentDispatcher on a worker-thread (and create the worker-thread's dispatcher) and I subscribe to events on that worker-thread, do I have to call Dispatcher.Run() in order to receive those events? – j00hi Feb 17 '11 at 22:54
  • @j00hi - Please refer to the MSDN documentation for Dispatcher.Run(): http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher.run.aspx. Basically, yes, you'll have to call Run in order to process messages. – Pavlo Glazkov Feb 18 '11 at 08:04
  • 1
    late reply, but your statement about the two threads is wrong: it's 1 for Rendering and 1 for all else: input, UI-update, etc. they are called: the "render thread" and the "UI thread" – Danahi May 23 '13 at 12:50
  • Adding to @Danahi's comment (which is right), this link gives more info: http://msdn.microsoft.com/en-us/magazine/cc163328.aspx – Jcl Jan 02 '15 at 12:03
12

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.

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
  • 1
    This code "Application.Current.Dispatcher..." just saved me hours... I couldn't figure out why this wasn't working until I saw you post. I see now that I was creating seperate Dispatchers instead of grabbing the Applications main Dispatcher... Thank you! – Jason Stevenson Apr 18 '11 at 02:09
  • Why does it call run after invoking shutdown? – StayOnTarget Aug 28 '20 at 13:22
  • @StayOnTarget That only runs when the Closed event executes, so it's not being called before Run (sorry for replying to an old comment). – NimbusHex Mar 29 '21 at 12:15
  • @NimbusHex thanks actually, I don't think anyone else had replied. And I totally missed that the Shutdown was within a delegate, spot on. – StayOnTarget Mar 29 '21 at 13:08
3

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.

vidstige
  • 12,492
  • 9
  • 66
  • 110
  • I've just tried the following and it did work, but I haven't called Dispatcher.Run(): `Dispatcher.CurrentDispatcher.Invoke(DispatcherPriority.Send, new WaitCallback(delegate(object state) { Console.WriteLine("Hello Dispatcher"); }), null);` Or did it only work because it is invoked on the same thread and it wouldn't work if it was invoked from another thread? – j00hi Feb 16 '11 at 11:25
  • 1
    It works because the current thread has a dispatcher running. The WPF framework will set this up for you automatically. Since the above code-snippet is executed in the UI thread the following will happen. The UI thread will return from the method - Perhaps you executed that code inside an event handler for a button for example. And after that the Dispatcher will see that there is an invoke to process and execute the Console.WriteLine() call. – vidstige Feb 16 '11 at 12:09