Dispatcher to Thread relationships in WPF

20,583

Solution 1

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.

Solution 2

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.

Share:
20,583
j00hi
Author by

j00hi

Software developer and software architect since the beginning of this century. Used to do a lot web, database and windows applications development but turned my focus to real-time graphics and game development in the past few years. In terms of programming language preferences, I like everything which is strongly typed and allows me to put more than pointers and integers on the stack. While I enjoy the beauty and light-heartedness of C#, the greatest programming language to me is (modern!) C++. It took me a while to get into it, but now I really appreciate its features and also its paradigms. My true passion is computer graphics and game development. I've done quite a lot of OpenGL development and gathered a lot of experience with Unity3D in the context of working on Augmented Reality applications and indie games. In the recent few years, I took a deep dive into Vulkan. I am working on a convenience and productivity layer atop Vulkan-Hpp and on a framework. Those two projects join two of the greatest technologies: Modern C++ and Vulkan.

Updated on July 18, 2022

Comments

  • j00hi
    j00hi almost 2 years

    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?