Dispatcher.CurrentDispatcher vs. Application.Current.Dispatcher

60,041

Solution 1

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.

Solution 2

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.

Solution 3

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.

Share:
60,041

Related videos on Youtube

ken
Author by

ken

Updated on May 01, 2020

Comments

  • ken
    ken about 4 years

    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?

  • ken
    ken about 12 years
    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?
  • Waihon Yew
    Waihon Yew about 12 years
    @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.
  • Admin
    Admin about 10 years
    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?
  • Waihon Yew
    Waihon Yew about 10 years
    @Will: One: "If your app only has one UI thread (most likely)...". Do you think it needs more clarification?
  • Admin
    Admin about 10 years
    @Jon: Well, considering it confused me, yeah. I didn't see the line you quoted...
  • Waihon Yew
    Waihon Yew about 10 years
    @Will: Edited to increase visibility of the relevant part. Thanks for letting me know.