views:

316

answers:

4

Well lets say i have an object that i databind to, it implements INotifyPropertyChanged to tell the GUI when a value has changed...

if i trigger this from a different thread than the GUI thread how would wpf behave?

and will it make sure that it gets the value of the property from memory and not the cpu cache?

more or less im asking if wpf does lock() on the object containing the property...

A: 

In practice it seems to work as expected and seems to be thread-safe (haven't seen anything odd happen or exceptions as a result of updating on background thread). I believe it invokes on to the UI thread when needed, but I'm not too familiar with the internals.

Davy8
A: 

The update must happen on UI thread otherwise app will crash.

ozczecho
+6  A: 

Value changes fired by INotifyPropertyChanged are automatically marshalled back onto the dispatcher. (http://blog.lab49.com/archives/1166)

Fire this event on any thread you like...


Value changes fired by INotifyCollectionChanged are NOT reliably marshalled onto the dispatcher. (http://csharplive.wordpress.com/2008/09/11/wpf-data-binding-observablecollection-cross-thread-binding-support/)

If you need to update an observable collection from a different thread, follow the advice in this link

Rob Fonseca-Ensor
+1 Ha! Learn a new thing everyday. Just did a quick test to confirm this....I am sure I had to marshall values to UI thread when working with wpf...maybe I am just going senile...
ozczecho
You certainly have to marshal any calls against an actual control (`textBox.Text="Foo")`, but the databinding framework helps a little.
Rob Fonseca-Ensor
What if i write a value to the private variable that the property uses and then fire the event, then the "GUI" thread goes about to read the value, but it has a old value cached on the CPU Cache... is there any handeling of this that makes sure that the value is not old or cached?...
Petoj
@Petoj Even if .Net didn't handle that for you, Databinding happens computer-YEARS after all other threads have finished calculating things. You'll be fine...
Rob Fonseca-Ensor
A: 

Value changes fired by INotifyCollectionChanged are NOT reliably marshalled onto the dispatcher. (http://csharplive.wordpress.com/2008/09/11/wpf-data-binding-observablecollection-cross-thread-binding-support/)

Wow, this is large overhead for thread safety. Is there a reason for not using the DispatcherObject.CheckAccess() approach? I use it and it works.

DHN