Reputation: 282885
I'm writing a download manager using C#/WPF, and I just encountered this error:
This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread.
The basic flow of my program is that a few web pages/downloads are enqueued at the start, and then they're downloaded asynchronously. When an HTML page has completed downloading, I parse it and look for more stuff to download, then enqueue it directly from within the worker thread.
I get that error when trying to send out the CollectionChanged
event on my customized queue class. However, I need to fire that event so that the GUI can get updated.
What are my options?
Upvotes: 2
Views: 1142
Reputation: 93434
You might want to look into the Reactive Extensions from Microsoft, they're designed for just this sort of thing. They create "observable collections" and are useful in paralell and multi-core scenarios.
UPDATE:
You may also find the SyncronizationContext useful as well. That's a Windows Forms sample, but there are variations for WPF as well.
Upvotes: 2
Reputation: 14877
Whenever I had to deal with collection change notifications across threads, I always turned to How can I propagate changes across threads blog article for help.
I am not sure from the context of your question if this can indeed help you, but if your collection is something you directly control, you can benefit from this.
Upvotes: 1
Reputation: 40759
You just need to make sure that you use the Dispatcher for the relevant thread to invoke any code that updates the collection, or fires the event.
http://msdn.microsoft.com/en-us/magazine/cc163328.aspx
Update
The Dispatcher is a property on all of the classes that derive from DispatcherObject, which includes all DependencyObjects, Visuals, etc.
So, your GUI objects will all have a Dispatcher property.
Upvotes: 1