Dispatcher.CurrentDispatcher vs. Application.Current.Dispatcher
Asked Answered
H

3

94

What are the differences between Dispatcher.CurrentDispatcher (in System.Windows.Threading) and Application.Current.Dispatcher (in System.Windows)?

My gut tells me that Application.Current.Dispatcher will never change and is global to all threads in the current application, while Dispatcher.CurrentDispatcher may create a new instance of Dispatcher depending on the thread from which it was called.

Is that correct?

If it is, is the purpose of Dispatcher.CurrentDispatcher primarily for multi-threaded UI?

Haversack answered 4/5, 2012 at 12:40 Comment(0)
F
117

My gut tells me that Application.Current.Dispatcher will never change and is global to all threads in the current application, while Dispatcher.CurrentDispatcher may create a new instance of Dispatcher depending on the thread from which it was called.

That is correct.

Additionally, there is no point whatsoever in accessing Dispatcher.CurrentDispatcher from a non-UI thread. It will do nothing unless you call Dispatcher.Run, and going into an infinite message loop is not what you want to be doing from within worker threads.

So:

  • In the most common scenario, where your app only has a single UI thread, Application.Current.Dispatcher and Dispatcher.CurrentDispatcher from within the UI thread will return the same instance. Which one you use is simply a matter of preference.

  • If your app has more than one UI thread then each DispatcherObject will be permanently associated with the dispatcher of the UI thread it was created in upon construction. In this case, Application.Current.Dispatcher will refer to the dispatcher of the thread your application spawned with; you will not be able to use it to post messages to controls owned by your other UI threads.

Freezedry answered 4/5, 2012 at 13:2 Comment(6)
Thanks for the explanation, but what do you mean by "It will do nothing unless you call Dispatcher.Run"? I've used CurrentDispatcher from a non-UI thread, and Invoke does indeed invoke a delegate. Do you mean that the delegates will simply be invoked on the calling thread?Haversack
@ken: Invoke and friends normally "package" (marshal, if you prefer) a method call into a Win32 message and post it to a message queue from which a message loop (the dispatcher loop) eventually extracts it and executes the call. Since there is no dispatcher loop (if you were in that loop your code would not be running) the call would never actually happen. So I assume that Invoke first checks if you are already in the CurrentDispatcher thread as an optimization and if you are executes the call on the spot instead of marshalling. You can verify this by checking Thread.ManagedThreadId.Freezedry
you will not be able to use it to post messages to controls owned by your other UI threads. So, how many UI threads are there in your average WPF application?Purgatorial
@Will: One: "If your app only has one UI thread (most likely)...". Do you think it needs more clarification?Freezedry
@Jon: Well, considering it confused me, yeah. I didn't see the line you quoted...Purgatorial
@Will: Edited to increase visibility of the relevant part. Thanks for letting me know.Freezedry
P
28

To put it simply...

Dispatcher.CurrentDispatcher gets the dispatcher for the current thread. So, if you're looking for the UI thread's Dispatcher from a background process, don't use this.

Application.Current.Dispatcher will always give you the UI thread's dispatcher, as this is the thread that spins up the sole Application instance.

Purgatorial answered 30/4, 2014 at 12:11 Comment(0)
A
10

My gut tells me that Application.Current.Dispatcher will never change and is global to all threads in the current application, while Dispatcher.CurrentDispatcher may create a new instance of Dispatcher depending on the thread from which it was called.

That is correct, the Application.Current.Dispatcher is an instance property of the application which is assigned upon construction to be the dispatcher of the current thread. And as the documentation of Dispatcher.CurrentDispatcher points out:

Gets the Dispatcher for the thread currently executing and creates a new Dispatcher if one is not already associated with the thread.


If it is, is the purpose of Dispatcher.CurrentDispatcher primarily for multi-threaded UI?

Possibly, i have not encountered any use in getting the dispatcher of a background thread as you usually have no UI-elments belonging to them to which you may want to dispatch operations.

Abduce answered 4/5, 2012 at 12:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.