2

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?

Wojciech Ketrzynski
  • 2,503
  • 2
  • 15
  • 12
  • 3
    Sounds like your are trying to make business classes invoke to the ui thread for you. This is always going to be problematic. – Gusdor Feb 13 '14 at 13:16
  • @Gusdor: It's ok to make business objects invoke the GUI if that is the design. Slow tasks should be done on different threads to keep it responsive; if those classes are defined in a different assembly, that's great, just add presentationframework to the references. Also call BeginInvoke too, as your thread will not have to wait for the call to finish before it continues. – Dean Mar 10 '16 at 14:57
  • @Dean Thanks for the advice but I respectfully disagree. If business code absolutely needs to be serialized onto the UI thread, or any execution queue for that matter, I will make it aware of an instance of `SynchronizationContext`. Business objects should never, ever be aware of the UI framework. How would you unit test that code with no `Application` initialized? What if you want to use that assembly in a desktop application _and_ a web service? You are going to be knee deep in refactoring because you didn't learn to use TPL. – Gusdor Mar 10 '16 at 15:07
  • @Gusdo: Fine but its hard to tell from the question what the business extent is. Using SynchronizationContext is more abstract but you have to pass the context around to all threads that need to us it. Using Application.Current.Dispatcher does not need this and also is a very popular method. – Dean Mar 10 '16 at 17:24
  • @DeanI Stephen Cleary wrote an awesome blog post on all of this stuff https://msdn.microsoft.com/en-us/magazine/gg598924.aspx Can you do me a big favour and read it through a few times? It is enlightening. – Gusdor Mar 10 '16 at 17:36

3 Answers3

3

why the class Application.Current.Dispatcher is not available? Have you referenced to the PresentationFramework assembly yet?

Toan Nguyen
  • 11,263
  • 5
  • 43
  • 59
1

You can get the Dispatcher from the Window or Current Window..

Application.Current.MainWindow.Dispatcher.Invoke() 
Sankarann
  • 2,625
  • 4
  • 22
  • 59
0

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.

Community
  • 1
  • 1
Miroslav Nedyalkov
  • 1,101
  • 1
  • 10
  • 22