Quantcast
Channel: MVVM – Dean Chalk
Viewing all articles
Browse latest Browse all 26

WPF & Background Threads – Have You Got It Wrong?

$
0
0

Just a quickie

You have MVVM and you have properties that notify via INotifyPropertyChanged.

Question : Do I have to use Dispatcher.BeginInvoke in order to change my bound properties in my ViewModel (i,e. when changing property values on background threads)

Answer: No. The Binding class will automatically marshal property changes via INotifyPropertyChanged onto the dispatcher for you automatically, and most importantly efficiently – by which I mean it evaluates the binding sources and targets and decide whether to marshal asynchronously or synchronously, and at what priority depending on other factors. This is half the reason for not bothering with MVVM frameworks like Caliburn, as they mostly perform tasks for you that don’t actually need to be performed.

Except: Some poorly written 3rd party controls (no names mentioned….. ‘cough – Infragistics’) assume that all data changes in the dependency system are always on the UI thread, and then tries to change dependency properties on the same thread (Dependency properties being changed directly MUST be updated on the UI thread). When using some of these controls you will have to marshal changes onto the UI thread just to support this buggy behavior.


Viewing all articles
Browse latest Browse all 26

Trending Articles