CollectionChanged notification across threads?

I am writing a download manager using C # / WPF and I just ran into this error:

This CollectionView type does not support changes to SourceCollection from a stream other than the Dispatcher stream.

The main flow of my program is that multiple web pages / downloads are logged at the beginning and then they are loaded asynchronously. When the HTML page has finished loading, I parse it and look for more material to load, then queue it up from the worker thread.

I am getting this error when trying to dispatch an event CollectionChanged

in my configured queue class. However, I need to fire this event so the GUI can update.

What are my options?

+2


a source to share


3 answers


You might want to look into Microsoft's Reactive Extensions , they are for this kind of thing. They create "observable collections" and are useful in concurrent and multi-core scenarios.

UPDATE:



You can also find SyncronizationContext . This is a Windows Forms sample, but there are also options for WPF.

+2


a source


You just need to make sure that you are using the dispatcher for the appropriate thread to invoke any code that updates the collection or fires an event.

http://msdn.microsoft.com/en-us/magazine/cc163328.aspx

Update



Dispatcher is a property for all classes that derive from DispatcherObject, which includes all DependencyObjects, Visuals, etc.

So your GUI objects will have a Dispatcher property.

+1


a source


Whenever I had to deal with collection change notifications in streams, I always turned to How can I propagate changes in streams ..

I'm not sure, in the context of your question, if this can really help you, but if your collection is something that you directly control, you can take advantage of that.

+1


a source







All Articles