Is there any way to get the UI thread's Dispatcher
when you have no reference to any UI elements?
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.
If you're in the main window you can use: Dispatcher. BeginInvoke(... Use Invoke if you want the current thread to wait until the UI thread has processed the dispatch code or BeginInvoke if you want current thread to continue without waiting for operation to complete on UI thread.
WPF Dispatcher is associated with the UI thread. The UI thread queues methods call inside the Dispatcher object. Whenever your changes the screen or any event executes, or call a method in the code-behind all this happen in the UI thread and UI thread queue the called method into the Dispatcher queue.
A Dispatcher is responsible for managing the work for a thread. The UI thread is the thread that renders the UI. The UI thread queues work items inside an object called a Dispatcher. The Dispatcher selects work items on a priority basis and runs each one to completion.
You can grab the UI Dispatcher from the static application instance: Application.Current.Dispatcher
You may want to check Application.Current
for null first, as it can be cleared during a shutdown sequence.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With