I have a wpf project where I am using INotifyPropertyChanged on a property which binds to the textbox. I am updating this value on a different thread using task (TaskParallelLibrary). It is updated properly and does NOT throw an exception. I was thinking it would throw an exception because it is running on a background thread and not UI thread. Ofcourse it is throwing an exception if I directly use the UI element. So, does INotifyPropertyChanged bind mechanism takes care of dispatching to the UI thread automatically?
Here is my code with the property.
private string _textProperty = "";
public string TextProperty
{
get
{
return _textProperty;
}
set
{
if (_textProperty != value)
{
_textProperty = value;
NotifyPropertyChanged("TextProperty");
}
}
}
and my task creation is
var task = new Task(() =>
{
TextProperty = "ABCD"; // Works.
// txtBox.Text = "ABCD"; // Throws an exception.
});
task.Start();
and the textbox in XAML is <TextBox Name="txtBox" Text="{Binding TextProperty}"/>