Dispatcher.CurrentDispatcher vs. Application.Current.Dispatcher

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.


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.


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.


Comments

  1. Axl

    • 2020/2/21

    My gut tells me that Application.Current.Dispatcher will never change and is global to all threads in the current application, while Dispatcher.

  2. Landen

    • 2021/5/30

    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.

  3. Jakari

    • 2020/4/16

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

  4. Dervishi

    • 2017/1/9

    member this.CurrentDispatcher : System.Windows.Threading.Dispatcher Public Shared ReadOnly Property CurrentDispatcher As Dispatcher Property Value Dispatcher. The dispatcher associated with the current thread. Remarks. If a Dispatcher is not associated with the current thread, a new Dispatcher will be created.

  5. Josue

    • 2019/12/26

    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 

  6. Braylon

    • 2018/9/26

    CurrentDispatcher vs Current.Dispatcher! Be ware of Dispatcher.CurrentDispatcher as my understanding of this is that is will return a Dispatcher for the current thread not the UI thread. Generally are you interested in the dispatcher on the UI thread - Application.Current.Dispatcher always returns this. Additional note:

  7. Liam

    • 2015/3/23

    CurrentDispatcher vs. Application.Current.Dispatcher. Apa perbedaan antara Dispatcher.CurrentDispatcher (Dalam System.Windows.Threading ) Dan Application.

  8. Kody

    • 2019/2/12

    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.

  9. Dustin

    • 2019/7/19

    CurrentDispatcher vs. Application.Current.Dispatcher. ¿Cuáles son las diferencias entre Dispatcher.CurrentDispatcher (en System.Windows.

  10. Alfonso

    • 2020/9/20

    Dispatcher.CurrentDispatcher obtiene el despachador para el hilo actual . Por lo tanto, si está buscando el despachador del subproceso de interfaz de usuario de un proceso en segundo plano, no lo use. Application.Current.Dispatcher siempre le dará el despachador del subproceso UI, ya que este es el hilo que activa la única instancia de la

  11. Ryker

    • 2017/2/18

    Application.Current.Dispatcher.BeginInvoke(. Invoke vs BeginInvoke. Use Invoke if you want the current thread to wait until the UI thread has processed the 

  12. Hank

    • 2020/1/7

    When a Dispatcher is created on a thread, it becomes the only Dispatcher that can be associated with the thread, even if the Dispatcher is shut down. If you attempt to get the CurrentDispatcher for the current thread and a Dispatcher is not associated with the thread, a Dispatcher will be created.

  13. Enrique

    • 2016/5/18

    Quais são as diferenças entre Dispatcher.CurrentDispatcher (em System.Windows.Threading) e Application.Current.Dispatcher (em System.Windows)?

  14. Luis

    • 2016/6/19

    public static Dispatcher Dispatcher => Application.Current?.Dispatcher ?? Dispatcher.CurrentDispatcher; That way it works both in the application and when running unit tests. I only had to use it in a few places in my entire application so I didn't mind repeating myself a bit.

  15. Thiago

    • 2018/8/28

    Current.Dispatcher niemals ändern wird und für alle Threads in der… CurrentDispatcher (in System.Windows.Threading ) und Application.Current.

  16. Harlan

    • 2017/7/26

    CurrentDispatcher vs. Application.Current.Dispatcher. Was sind die Unterschiede zwischen Dispatcher.CurrentDispatcher (im System.Windows.

Comments are closed.

Recent Posts