Danielle
Danielle

Reputation: 317

How to make Observable collection work in C# Net Threads?

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

Answers (1)

dev hedgehog
dev hedgehog

Reputation: 8791

Do not use MTObservableCollection.. Instead use BindingOperations.EnableCollectionSynchronization.

Upvotes: 1

Related Questions