Multi-threading, ObservableCollection, Reactive Extensions

wpf observable

We are all familiar with the MVVM scenario where we add items to WPF control via an ObservableCollection in our ViewModel.
Here’s a quick set of simple code to demonstrate our starting point for this post.

And as you’d expect, here is the output

Now, lets try and add a little multi-threading and see what happens

As you can see, we are now adding to our ObservableCollection on a background thread, and when click our button, we get this error.
This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread.
The more experienced among you will recognise this issue, and know that controls, dependency properties and some other constructs (including ObservableCollection) can only be updated on the main UI thread.
So many of you will try this solution where you marshal the change on to the UI thread via the Dispatcher, which works in most scenarios

However, you will still get the same error:
This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread.
You may think to yourself “Ive added to the collection via the Dispatcher thread, so why am i still getting exceptions?”. The reason you are still getting exceptions is because although you’ve added an item to the collection via the Dispatcher, the CollectionChanged event is called within the ObservableCollection on the background thread, and there is no way to avoid this in this kind of scenario.
A lot of solutions for this type of scenario include creating custom ObservableCollection classes that guarantee to throw the CollectionChanged event on the Dispatcher thread, but that only adds to your code base, and adds to the lines of code in your solution that need to be maintained and tested.
A simple alternative solution is to use Reactive Extensions.
Reactive Extensions (or Rx as they’re sometimes called) are a Microsoft Library that you can download and include in your project, that make utilising multi-threaded data-feeds in your app much easier and more reliable (I wont go into the full details here, there’s plenty of info online).
Although Reactive Extensions’ primary purpose is to ‘react’ to data in motion, we can use it here to solve our problem. Here is a modified ViewModel that makes use of Rx to avoid threading issues in this scenario.

As you can see, the reactive extension implementation has allowed us to add items to our collection from a background thread without any errors.

About the author

Dean Chalk

I am a freelance (contract) Microsoft WPF developer, specialising in challenging, high-performance user interface development. I have been working exclusively on enterprise WPF app development since 2006, and I have been a part of some of the most demanding WPF projects ever created withing the Investment Banking / trading industry.


>