I've solved some problem with a slow UI update, and I want to hear an explanation why my code modification actually improved the performance.
I develop a WPF application, MVVM Architecture. I have a property in the view model:
public DateTime MyDate
{
get
{
return _myDate;
}
set
{
_myDate= value;
OnPropertyChanged()
OnPropertyChanged(() => MyDateFormatted);
}
}
and the MyDateFormatted looks as follows:
public string MyDateFormatted
{
get { return _myDate.ToString("MMMM dd, yyyy"); }
}
The setter of MyDate does NOT happen on the UI Thread. The getter of MyDateFormatted happens on the UI Thread, because, as I've read, WPF automatically marshals property changes to the UI thread.
Making sure OnPropertyChanged() is called on UI thread in MVVM WPF app
And indeed, the UI was updated, but very slowly. Once, I've manually attached the OnPropertyChanged(() => MyDateFormatted); to the UI Dispacher, by surrounding it with the invoke: Application.Current.Dispatcher, the UI updates now as quickly as I wanted, and the performance has improved dramatically. Can you please explain why? Thank you!