We pay for user submitted tutorials and articles that we publish. Anyone can send in a contributionLearn More
A while ago Shahar wrote an article about whether WPF Data Binding is Thread safe. Shahar’s findings were that Even if you change a property from a different thread the PropertyChanged event will be called on the UI Thread making Binding Thread Safe.
I have created a Window with 2 TextBlocks, one of them is binded to a Dependency Property and the other is binded to a regular property:
The Code Behind:
As you can see Clicking the Button creates a Thread that changes both Properties.
And now the results!
If you read Shahar’s post you know that the regular Property works, but on the Dependency Property we get the following Exception:
It does not work!
Dependency Properties are not thread safe, but why?
If we look at the syntax of the Dependency Property declaration we see that we pass 4 parameters Name, Type, OWNER and Initial Value. I have specified the Window as the owner which means that to the Framework, this Dependency Property is the same as any Button, TextBlock or any other UI Element which is Owned by the UI Thread. By Trying to change it from the Thread it was as if I was trying to change the UI itself.
So always remember, a Dependency property is a UI element as far as Threading goes
Copyright © 2012 Dev102.com
Breeze : Designed by Amit Raz and Nitzan Kupererd