Reputation: 317
I was trying to implement a loading icon in my WPF application (thru threading) but hit a Null Reference exceptions when I tried to add items into observable collection. I followed the following suggestion but it does not work: How to make ObservableCollection thread-safe?
In short:
This works:
private void Refresh() {
MTObservableCollection <someObject> someTable = new MTObservableCollection<someObject>();
someTable.Add(new someObject());
...
}
This does not work:
private void Refresh()
{
Task.Factory.StartNew(()=> {
MTObservableCollection <someObject> someTable = new MTObservableCollection<someObject>();
someTable.Add(new someObject()); //this line throws null reference for someTable;
});
...
}
The reason I used Task.Factory is because I want to put in a loading icon while this happen.
Upvotes: 0
Views: 166
Reputation: 8791
Do not use MTObservableCollection.. Instead use BindingOperations.EnableCollectionSynchronization.
Upvotes: 1