Click here to Skip to main content
15,123,585 members
Articles / Desktop Programming / WPF
Posted 2 Jul 2012

Tagged as


11 bookmarked

Thread Safe Improvement for ObservableCollection

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
2 Jul 2012CPOL2 min read
Why didn’t Microsoft provide this in the ObservableCollection?


I was working on a WPF project where I had the ViewModel mirroring the Model displaying a hierarchical view of the data (like a tree view, but using XamDataGrid). I wanted to have an event in the Model that triggered when a record was added or removed to reduce coupling (probably when changes are made in the Model in the future). This was because I expected to be using the Model side with many different ViewModels, although at this point not sure since I am being given the requirements piece-meal. When I did this in a case where I had a BackgroundWorker, I started having problems with being on the wrong thread. Of course I expected that, and I immediately implemented a Dispatcher on the receiving class using code like the following:

Dispatcher.CurrentDispatcher.Invoke(new Action<StagedBlotterOrderAggViewModel>(Add), adding);

This did not work so I attempted to use DispatcherPriority. Nothing looked right to me, but I tried several options and none worked.

I could go back to a design of trying to ensure that the record updates were done in the BackgroundWorkerCompleted handler, but that would complicate the code.

Of course I know that you can get the Dispatcher from the control, but that was not very elegant. Therefore I started to search the web, and found something interesting at “Have worker thread update ObservableCollection that is bound to a ListCollectionView” ( I was not sure it would work, but I tried it, and it did. Of course I guess eventually I would have come up with inheriting from ObservableCollection<T> since I knew that I could get the Dispatcher from the control and then would not have had to do anything special in the ViewModel, but I am sure this is better than what I would have come up with. I did make a small number of improvements of adding the other constructors.

Here is my slightly improved version of this code that worked so well for me:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Windows.Threading;
namespace Custom.Collections
  public class ObservableCollectionEx<t> : ObservableCollection<t>
    // Override the event so this class can access it
    public override event NotifyCollectionChangedEventHandler CollectionChanged;
    public ObservableCollectionEx(IEnumerable<t> collection) : base(collection) { }
    public ObservableCollectionEx(List<t> collection) : base(collection) { }
    protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
      // Be nice - use BlockReentrancy like MSDN said
      using (BlockReentrancy())
        var eventHandler = CollectionChanged;
        if (eventHandler != null)
          Delegate[] delegates = eventHandler.GetInvocationList();
          // Walk thru invocation list
          foreach (NotifyCollectionChangedEventHandler handler in delegates)
            var dispatcherObject = handler.Target as DispatcherObject;
            // If the subscriber is a DispatcherObject and different thread
            if (dispatcherObject != null && dispatcherObject.CheckAccess() == false)
              // Invoke handler in the target dispatcher's thread
                            handler, this, e);
            else // Execute handler as is
              handler(this, e);

Now my big question is: Why didn’t Microsoft provide this in the original ObservableCollection?


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Clifford Nelson
Software Developer (Senior) Clifford Nelson Consulting
United States United States
Has been working as a C# developer on contract for the last several years, including 3 years at Microsoft. Previously worked with Visual Basic and Microsoft Access VBA, and have developed code for Word, Excel and Outlook. Started working with WPF in 2007 when part of the Microsoft WPF team. For the last eight years has been working primarily as a senior WPF/C# and Silverlight/C# developer. Currently working as WPF developer with BioNano Genomics in San Diego, CA redesigning their UI for their camera system. he can be reached at

Comments and Discussions

BugA race condition can result in the UI displaying each item twice Pin
Dave Mackersie4-Nov-15 7:16
MemberDave Mackersie4-Nov-15 7:16 
AnswerRe: A race condition can result in the UI displaying each item twice Pin
Clifford Nelson4-Nov-15 9:54
MemberClifford Nelson4-Nov-15 9:54 
Questionperformance Pin
kadzbi26-May-14 0:24
Memberkadzbi26-May-14 0:24 
QuestionChange made in .Net 4.5.1 (at least) Pin
BC3Tech16-May-14 8:32
MemberBC3Tech16-May-14 8:32 
According to the online reference

this is now in ObservableCollection<t>:
/// <summary>
/// Raise CollectionChanged event to any listeners.
/// Properties/methods modifying this ObservableCollection will raise
/// a collection changed event through this virtual method.
/// </summary>
/// <remarks>
/// When overriding this method, either call its base implementation
/// or call <see cref="BlockReentrancy"/> to guard against reentrant collection changes.
/// </remarks>
protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
    if (CollectionChanged != null)
        using (BlockReentrancy())
            CollectionChanged(this, e);

AnswerRe: Change made in .Net 4.5.1 (at least) Pin
Clifford Nelson4-Nov-15 9:54
MemberClifford Nelson4-Nov-15 9:54 
QuestionNot thread-safe Pin
Michael Bendtsen4-Dec-12 3:25
MemberMichael Bendtsen4-Dec-12 3:25 
AnswerRe: Not thread-safe Pin
Clifford Nelson4-Dec-12 6:20
MemberClifford Nelson4-Dec-12 6:20 
QuestionNice try Pin
Jasper4C#3-Jul-12 3:54
MemberJasper4C#3-Jul-12 3:54 
AnswerRe: Nice try Pin
FatCatProgrammer3-Jul-12 5:11
MemberFatCatProgrammer3-Jul-12 5:11 
AnswerRe: Nice try Pin
Clifford Nelson3-Jul-12 7:54
MemberClifford Nelson3-Jul-12 7:54 
AnswerRe: Nice try Pin
Clifford Nelson3-Jul-12 7:50
MemberClifford Nelson3-Jul-12 7:50 
QuestionReponse to your question Pin
Nicolas Dorier3-Jul-12 2:49
professionalNicolas Dorier3-Jul-12 2:49 
GeneralRe: Reponse to your question Pin
Clifford Nelson3-Jul-12 7:59
MemberClifford Nelson3-Jul-12 7:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.