1

In WPF, is it true that every dispatcher has its own thread? From the description of Dispatcher.BeginInvoke:

Executes a delegate asynchronously on the thread the Dispatcher is associated with.

we know that a dispatcher can only be associated at most one thread. Can two different threads share one dispatcher? If I understand correctly, Dispatcher is an abstraction controlling a message pump, i.e. an application can send messages to the Dispatcher, and it takes care of executing them on its thread. Correct me if I'm wrong.

user4205580
  • 564
  • 5
  • 25
  • There is a one to one relationship between a thread and a dispatcher. That said, multiple threads *can* queue work on the UI threads dispatcher (e.g In WPF you use `Application.Current.Disptacher`). You actually have to do that if you want to interact with UI elements from a worker thread. – Yuval Itzchakov Nov 21 '15 at 12:08

2 Answers2

1

Yes. However you have to understand the fact That there can be and always is only one UI thread. And as there is a one to one relationship between the thread and the dispatcher, it means there is only one dispatcher for the (one) UI thread.

I.e. There can't be two threads associated with a single dispatcher, as there is only one UI thread.

Don
  • 6,632
  • 3
  • 26
  • 34
0

According to MSDN, Dispatcher.CurrentDispatcher;

If a Dispatcher is not associated with the current thread, a new Dispatcher will be created. This is not the case with the FromThread method. FromThread will return null if there is not a dispatcher associated with the specified thread.

If you interpret that text a Thread owns (or is associated with) one dispatcher, and each association between the two are 1-1, since there is no setter for the Dispatcher in the Thread class.

Patrick
  • 17,669
  • 6
  • 70
  • 85