I have one query, in Winform (UI Thread) control Invoke is there to handle cross thread issue, then why Dispatcher is introduced in WPF? What are the other advantages of Dispatcher?
Please help me understanding this.
I have one query, in Winform (UI Thread) control Invoke is there to handle cross thread issue, then why Dispatcher is introduced in WPF? What are the other advantages of Dispatcher?
Please help me understanding this.
Copyright © 2021 Jogjafile Inc.
The main reason
Dispatcher
was introduced in WPF is that WPF isn't WinForms and WinForms class members aren't relevant to WPF objects. They had to do something.Could Microsoft have implemented WPF exactly the same way as they did WinForms? Sure. But WPF is a fundamentally different kind of API, and it makes sense that they would take the opportunity to redesign the paradigm that deals with the thread affinity that naturally occurs when writing any Windows software, managed or otherwise.
Note that WPF, and the Dispatcher in particular, are higher-level abstractions than WinForms and its
Invoke()
method. The application has more control over theDispatcher
than a WinForms program would overInvoke()
. TheDispatcher
supports different priorities, and even supports exceptions forDispatcherObject
s that can safely be used in different threads (seeFreezable
).At the end of the day, WPF's
Dispatcher
is doing much the same job as WinForm'sInvoke()
method. It's just a bit shinier and offers more nuanced mechanisms for accomplishing the same fundamental goal.