Introduction

I ran across a web-page on the internet where people were voting for enhancements to the next .NET Framework. On the page, I noticed there were a lot of people voting for a safe multi-threaded way of dispatching an INotifyCollectionChanged event, mostly regarding the ObservableCollection. That made me remember the initial pains I had with this issue and the many articles devoted to trying to remedy it, one of the earliest if not first solutions coming from Bea Stollnitz (love her blog). I have long since put this behind me since I solved it on my own a few years ago but completely forgot that it's still very much the pain in the neck it was back then, so I've decided to pull the relevant classes out of my library and share it with everyone.

The good thing about my solution is that it's simple doesn't have the limitations of most of the other solutions I've seen such as the inability to modify the collection in certain threads, the inability to databind to the collection from other UI threads, the need to keep two collections internally and keep them in sync, or the necessity of passing in a Dispatcher, etc... My solution allows you to modify the collection by *any* thread whether it's the owner UI thread, some other UI thread that isn't the owner, or a background thread.

Please take your time to read this article so that you can understand some of the problems I intend to deal with and let me know whether or not this solution was helpful to you as well as any bugs you may have encountered. Also, don't forget to vote!

Major Update : In the previous version of this article I solved the problem that resulted in the infamous "This type of CollectionView ..." exception but my job wasn't really done. You see, there are other issues as to why you can't update a collection from another thread that will cause exceptions and I hadn't addressed those. One major problem is that the UI thread may be trying to display your collection and while it's in the middle of displaying it on the screen a background thread might remove an item without the UI thread knowing about it and when it tries to display the item it finds it doesn't exist and throws an exception. Another major problem is that two or more threads might try to add or remove items at the same time and end up corrupting the collection resulting in an exception. I have now addressed these other issues as well. The main class I will be dealing with in this article is the ObservableImmutableList class because this class is the one that will replace the ObservableCollection class you would normally use. However, my library now includes other collection classes (such as ObservableImmutableDictionary) that share the same thread-safety properties and now implement INotifyCollectionChanged as well. I will keep expanding this library as I find the time to do so.

Background

There are already numerous articles out there detailing the issue in depth, so I'll only give a brief synopsis here. The problem, in a nutshell, is that the INotifyCollectionChanged event, when triggered from a thread other than the originating thread, will give you the infamous "This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread." You can test this yourself by creating an ObservableCollection and binding it to an ItemsControl/ListBox/ListView; note that when you do this, you are doing it on the UI thread. Then, add an item to it via a worker thread, such as through a Timer event or ThreadPool.QueueUserWorkItem and you'll get an exception. The problem is that the NotifyCollectionChanged event (the one your ItemsControl/ListBox/ListView automagically subscribes to in order to determine if something has been added or removed from the collection) gets triggered from the worker thread because the worker thread is trying to modify the collection and since the worker thread isn't a UI thread it throws an exception. This explanation is a tad-bit simplified, but you get the idea.

Using the Code

The magic to solving the "This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread." problem in a simple manner happens when you realize that the problem only occurs at the notification side. If I want to be notified of an event, what would I have to do? I would have to subscribe to it, of course. This is where the magic happens; it happens during the subscription process.

What I've done is override the CollectionChanged subscription mechanism so that whenever any changes occur to the collection, it will trigger it from the relevant thread.

As you can see I am getting the DispatcherObject associated with each event handler and if it's a UI thread then I use it to marshall the call to the UI thread; if not then it's a background thread and I simply execute that on the spot. That's all that's needed to fix that infamous exception. Now in order to resolve the other thread-safety issues I needed to make sure that 1) changes that are made to the collection occur atomically (that is, they occur in one fell-swoop without moving the ground from underneath you) and 2) that no further changes can occur while the NotifyCollectionChanged event is executing on each thread.

The solution to problem #1 is to change the underlying data-structure from an ObservableCollection to an ImmutableList and add Observable functionality to it. The reason for this is that the ObservableCollection is full of state that changes underneath you while the ImmutableList is immutable and can't change underneath you; if you need to add or remote an item from an ImmutableList it creates another ImmutableList for you (which is how strings work). Now you may think this may incur a huge performance hit but it's actually doing some very clever things behind the scenes so it's pretty fast.

The solution to #2 was to wrap the modification of the collection and the NotifyCollectionChanged in a lock so that for every change that occurs each thread is immediately and synchronously notified before any further changes can occur. Now the problem with locks is that they're slow but that's nothing compared to the major problem with locking... If a background thread obtains the lock and proceeds to modify the collection it will then call each NotifyCollectionChanged event on their respective threads. However, if the UI thread it's trying to call is blocking (ie. waiting) on the lock then you run into a deadlock; that is, the background thread is calling the UI thread and waiting for it to execute the NotifyCollectionChanged event but the UI thread is blocking on the lock that the background thread took so it can't execute anything and you end up with both threads waiting for each other and stuck for all eternity. To solves the speed issue I allow the user to specify (via the LockType property) whether they want to use a SpinWait lock or a Monitor lock; SpinWait is a lockless lock that is good for scenarios in which your NotifyCollectionChanged logic doesn't take too long while Monitor is a lock that is good for scenarios in which your NotifyCollectionChanged logic may be doing some heavy lifting. In either case you still end up blocking the UI thread so I implemented a locking mechanism specifically for the UI so that while it's trying to acquire the lock it's still pumping messages; this was accomplished using the Dispatcher and DispatcherFrame.

it will keep trying to acquire a lock until the condition is met and while it's doing so it will continue processing UI messages. It is able to do this because it created a message-pump within the original message-pump that can still process messages while it's busy acquiring the lock.

One important thing to note is that you can no longer use any of the modification methods you're used to using such as Add, Remove, etc... because they are not thread-safe. I have supplied thread-safe methods that you should be using called TryOperation and DoOperation. TryOperation will attempt to perform the operation once and if it succeeds it will return true but if it is unable to acquire the lock because some other thread is trying to make changes to it then it will return false. DoOperation works pretty much the same way except that it keeps trying until it succeeds. Here is an example of using DoOperation to remove an item :

Here I am attempting to remove an item at index 0 but other threads might be trying to do the same thing! Both TryOperation and DoOperation offers me thread-safe ImmutableList that I should be performing all my modifications on and in this example I name it 'currentItems'. Since other threads can be doing things to the collection at the same time I need to check whether or not the 'current' list has an item count greater than zero and if it does then I can safely remove the item at index zero. If the count is zero then there are no items to remove and I simply return 'null' which tells the DoOperation to cancel the operation. You can actually perform as many changes to ImmutableList as you want in a thread-safe manner :

There are a couple of caveats : 1) Don't modify the collection within the NotifyCollectionChanged event! You can read it just fine just don't try to modify it or you'll end up in hell; just trust me on this. 2) There's something about the VirtualizingStackPanel that doesn't play well with this class so you can't use it, sorry; instead replace it with a StackPanel like I did, just check out my xaml in the uxViewWindow class. If anyonce can figure out what it is about the VirtualizingStackPanel that poops on my parade I'd love to hear it.

Attached you will find a Visual Studio 2012 solution that contains a test project so that you can see the class at work. When you run the test solution, the main window will appear.

The main window consists of an ObservableImmutableList, an ItemsControl bound to the collection, and a button for spawning separate views of the same data. If you look at the window title, you will see the thread ID associated with the window (i.e., the UI Thread ID). The main window has an internal timer that triggers every 100 milliseconds; every time it gets triggered it will add, remove, or change a string in the list and it will do this from either the UI thread or a background thread. In the above snapshot, you can see that even though the ItemsControl is bound to the collection on Thread1, worker threads 5 and 6 are both able to add items to the collection without a problem.

Another benefit of using the ObservableImmutableList is that you're not limited to just one UI control; you can bind *multiple* UI controls to it. You can even get your own custom ICollectionView instance from it using the CollectionView class! For those of you that don't know about views, the concept is simple: each view is simply another way of looking at the exact same data. For example, imagine that you have an address book containing the names, phone numbers, and addresses of all your friends. Now imagine that you want to have three windows showing your address book: one window to display them sorted by last name, another window to group them by state, and another window to edit the address book. Wouldn't it be a pain to pass the first window a copy of your address book sorted by last name, the second window a copy grouped by state, and the third window an editable instance of your address book? There's really no need for that with views since each view abstracts the presentation from the source. However, experimenting with all the great things you can accomplish with views is a topic for another article and not the point of my demo; I simply want to let you know that my ObservableImmutableList supports it and affords it the same multi-threaded protection.

If you click on the "Open View On Separate Thread" button, you will open another window that is on a completely different thread. Click on this button twice and you will get the following:

What you're seeing are three windows, each on completely different UI threads, each displaying a different view of the same data.

As you can see the timer able to add/remove/modify items to the ObservableImmutableList from either the background or UI thread with no exceptions whatsoever.

Please feel free to dissect the code any which way you like; I hope this has saved you many months of nail-biting agony. If you have any questions, please feel free to ask, and don't forget to rate this article below!

Points of Interest

As I wrote this article, I came across another solution very similar to mine by Muljadi Budiman at geekswithblogs; essentially the same approach except that he gets the dispatcher from the invocation list while I saved mine manually. I have to give it to him, the thought of getting the dispatcher from the invocation list never occurred to me and I have since updated my code to use his approach which results in less lines of code. You can find his article here (note : his solution only resolves the infamous problem I mentioned earlier and does not address the ObservableCollection's inherent thread-safety issues)

History

March 11, 2010: Initial post

March 12, 2010: Added snapshots and information related to the demo application

March 14, 2014: Major update to fix the remaining thread-safety issues.

This is excellent piece of code! I have managed to extend it with ReadOnlyObservableImmutableList (just refactored ReadOnlyCollection code) and ConcurrentRingBufferWithObservation. One slight problem that I have is that if you set demo timer to extreme small value (like 10ms) it will randomly die with something like 'An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll
Exception has been thrown by the target of an invocation.'.

UPDATE: Inner exception was "Added item does not appear at given index '0'.". It seems that when Adding/Removing fast (ring-buffer emulation) some collection change notifications are too late.

When using the AddRange function, the items in the ObservableImmutableDictionary<String, String> is not in the same order as the input data. (Dic is a Dictionary. Attributes is an ObservableImmutableDictionary)

Dictionaries don't have any specific order so you shouldn't count on them being ordered. For example, the Dictionary dic you use to populate attributes is not in the same order it was created. Try creating a new Dictionary and adding key/values to it and see if it remains in the same order as the input data and you'll see.

- LockTypeEnum.Lock doesn't work
-> to prove simply change lock type in MainWindow -> soon after start at least one window freezes (btw: checking whether a lock was taken in Unlock shows it wasn't, even after a Lock call which unconditionally must have taken a lock and must keep it 'til Unlock!)

=> The locking part seems unreliable and unsafe to me.
=> To show and prove correctness tests (besides demos) would be helpful, but are missing completely.

This collection *is* thread-safe and provides you with everything you need to perform thread-safe operations via the TryOperation and DoOperation methods, you just need to read the article. I've provided solutions to all your questions via email but it's become increasingly clear that you're not reading the article at all and that you'd rather I did all the coding for you. *Please read the article!* If you need to perform a thread-safe operation then do it within the DoOperation or TryOperations methods. You're getting errors because you're approaching the problem from a single-threaded perspective. Within DoOperation you must perform due-diligence by checking the collection to see if the element you are trying to update exists; if it doesn't then another thread must have removed it! Make sure the 10th item is there first and *then* update it!

When running this with a monitor type lock instead of a spinner, after opening about 3-4 windows, everything comes to a halt. Also, you may want to switch to ListCollectionView. Other than that, this is a great idea and has a lot of potential. Thanks for sharing!

--
"Keyboard not found. Press &lt; F1 &gt; to RESUME. "
Source unknown (appears in many common BIOSes as a real error message)

You are absolutely correct, the monitor lock is only there for certain rare scenarios but definitely not for the type of scenario my demo represents; the default lock type is the spin lock which is what should be used most of the time. Having different lock types was an idea that came to me at the last minute so I think I may eventually refactor this code to allow you to specify your own custom lock mechanism but I've been rather busy lately.

As for ListCollectionView I used the more generic CollectionView in my demo in order to remain compatible with other types of collections that I add to the library that do not implement IList (such as the ObservableImmutableDictionary type I provided) should I decide to demo them as well.

First, I have to admit, I didn't finish your article, I'm a little too groggy to fully understand it now. I did read enough to know I completely understood what you were saying, the problems I didn't know enough to want to avoid, and have no fear you'll lose me in the future. I very much appreciate your writing style. Thanks