Reputation: 2661
I want to get current dispatcher in WPF application. The code where I want to get the dispatcher is in the .cs
class file(regular C# class).
Application.Current.Dispatcher.Invoke()
and other ....Current.Dispatcher.Invoke()
are not available.
Is there any way to do so?
Upvotes: 2
Views: 6013
Reputation: 2665
You can get the Dispatcher from the Window or Current Window..
Application.Current.MainWindow.Dispatcher.Invoke()
Upvotes: 1
Reputation: 1121
I think System.Windows.Threading.Dispatcher.CurrentDispatcher should do the job, but you should be aware that the Dispatcher you will receive depends on the thread you are calling this method from. Here you can see very good explanation what's the exact difference between the two - 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's why I would advise you to find a way to abstract Application.Current.Dispatcher or the Dispatcher of some view and to access it through this abstraction - it is just safer for multithreading scenarios.
Upvotes: 0
Reputation: 11601
why the class Application.Current.Dispatcher is not available? Have you referenced to the PresentationFramework assembly yet?
Upvotes: 3