I have some strange problem in my WPF app.
I’m using a MVVM pattern and this is the part of my MainWindowViewModel:
// GridView control in MainWindow.xaml binded to this property
public DataTable DT
{
get { return _dt; }
}
// INotifyPropertyChanged Member for refreshing bindings
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
// my function
void OnCreateTable()
{
_dt = // creating new table here
OnPropertyChanged("DT"); // refresh binding
}
When I call OnCreateTable() program almost allways hangs up with 100% CPU usage (sometimes with no CPU usage but others errors like incorrect data in GridView control).
While debugging I discovered some facts:
1) OnCreateTable() and data bindings work fine if make pause before OnPropertyChanged:
void OnCreateTable()
{
_dt = // creating new table here
Thread.Sleep(1000); //!!!
OnPropertyChanged("DT"); // refresh binding
}
2) OnCreateTable() and data bindings work fine if trace it with “step over” (because this makes pause before OnPropertyChanged too)
I can’t understand why I need to make pause before OnPropertyChanged.
I think I found the problem. Sorry, I forgot that I have added the property name checking:
I can’t understand why, but calling VerifyPropertyName() needs a pause, or it results in that bug, I wrote.
If I remove call to VerifyPropertyName() all works propertly!