ThreadSafeObservableCollection<T>

As part of an ongoing article I am just about to finish, I needed a thread safe ObservableCollection<T>. Now the native .NET framework doesn’t have one of those, but luckily it supplies you with the right job to create one using some of the Threading APIs.

Here is what I came up with.

 

   1:  /// <summary>
   2:  /// Provides a threadsafe ObservableCollection of T
   3:  /// </summary>
   4:  public class ThreadSafeObservableCollection<T>
   5:      : ObservableCollection<T>
   6:  {
   7:      #region Data
   8:      private Dispatcher _dispatcher;
   9:      private ReaderWriterLockSlim _lock;
  10:      #endregion
  11:  
  12:      #region Ctor
  13:      public ThreadSafeObservableCollection()
  14:      {
  15:          _dispatcher = Dispatcher.CurrentDispatcher;
  16:          _lock = new ReaderWriterLockSlim();
  17:      }
  18:      #endregion
  19:  
  20:  
  21:      #region Overrides
  22:  
  23:      /// <summary>
  24:      /// Clear all items
  25:      /// </summary>
  26:      protected override void ClearItems()
  27:      {
  28:          _dispatcher.InvokeIfRequired(() =>
  29:              {
  30:                  _lock.EnterWriteLock();
  31:                  try
  32:                  {
  33:                      base.ClearItems();
  34:                  }
  35:                  finally
  36:                  {
  37:                      _lock.ExitWriteLock();
  38:                  }
  39:              }, DispatcherPriority.DataBind);
  40:      }
  41:  
  42:      /// <summary>
  43:      /// Inserts an item
  44:      /// </summary>
  45:      protected override void InsertItem(int index, T item)
  46:      {
  47:          _dispatcher.InvokeIfRequired(() =>
  48:          {
  49:              if (index > this.Count)
  50:                  return;
  51:  
  52:              _lock.EnterWriteLock();
  53:              try
  54:              {
  55:                  base.InsertItem(index, item);
  56:              }
  57:              finally
  58:              {
  59:                  _lock.ExitWriteLock();
  60:              }
  61:          }, DispatcherPriority.DataBind);
  62:  
  63:      }
  64:  
  65:      /// <summary>
  66:      /// Moves an item
  67:      /// </summary>
  68:      protected override void MoveItem(int oldIndex, int newIndex)
  69:      {
  70:          _dispatcher.InvokeIfRequired(() =>
  71:          {
  72:              _lock.EnterReadLock();
  73:              Int32 itemCount = this.Count;
  74:              _lock.ExitReadLock();
  75:  
  76:              if (oldIndex >= itemCount |
  77:                  newIndex >= itemCount |
  78:                  oldIndex == newIndex)
  79:                  return;
  80:  
  81:              _lock.EnterWriteLock();
  82:              try
  83:              {
  84:                  base.MoveItem(oldIndex, newIndex);
  85:              }
  86:              finally
  87:              {
  88:                  _lock.ExitWriteLock();
  89:              }
  90:          }, DispatcherPriority.DataBind);
  91:  
  92:  
  93:  
  94:      }
  95:  
  96:      /// <summary>
  97:      /// Removes an item
  98:      /// </summary>
  99:      protected override void RemoveItem(int index)
 100:      {
 101:  
 102:          _dispatcher.InvokeIfRequired(() =>
 103:          {
 104:              if (index >= this.Count)
 105:                  return;
 106:  
 107:              _lock.EnterWriteLock();
 108:              try
 109:              {
 110:                  base.RemoveItem(index);
 111:              }
 112:              finally
 113:              {
 114:                  _lock.ExitWriteLock();
 115:              }
 116:          }, DispatcherPriority.DataBind);
 117:      }
 118:  
 119:      /// <summary>
 120:      /// Sets an item
 121:      /// </summary>
 122:      protected override void SetItem(int index, T item)
 123:      {
 124:          _dispatcher.InvokeIfRequired(() =>
 125:          {
 126:              _lock.EnterWriteLock();
 127:              try
 128:              {
 129:                  base.SetItem(index, item);
 130:              }
 131:              finally
 132:              {
 133:                  _lock.ExitWriteLock();
 134:              }
 135:          }, DispatcherPriority.DataBind);
 136:      }
 137:      #endregion
 138:  
 139:      #region Public Methods
 140:      /// <summary>
 141:      /// Return as a cloned copy of this Collection
 142:      /// </summary>
 143:      public T[] ToSyncArray()
 144:      {
 145:          _lock.EnterReadLock();
 146:          try
 147:          {
 148:              T[] _sync = new T[this.Count];
 149:              this.CopyTo(_sync, 0);
 150:              return _sync;
 151:          }
 152:          finally
 153:          {
 154:              _lock.ExitReadLock();
 155:          }
 156:      }
 157:      #endregion
 158:  }

It relies on this small extension method

 

   1:  /// <summary>
   2:  /// WPF Threading extension methods
   3:  /// </summary>
   4:  public static class WPFControlThreadingExtensions
   5:  {
   6:      #region Public Methods
   7:      /// <summary>
   8:      /// A simple WPF threading extension method, to invoke a delegate
   9:      /// on the correct thread if it is not currently on the correct thread
  10:      /// Which can be used with DispatcherObject types
  11:      /// </summary>
  12:      /// <param name="disp">The Dispatcher object on which to do the Invoke</param>
  13:      /// <param name="dotIt">The delegate to run</param>
  14:      /// <param name="priority">The DispatcherPriority</param>
  15:      public static void InvokeIfRequired(this Dispatcher disp,
  16:          Action dotIt, DispatcherPriority priority)
  17:      {
  18:          if (disp.Thread != Thread.CurrentThread)
  19:          {
  20:              disp.Invoke(priority, dotIt);
  21:          }
  22:          else
  23:              dotIt();
  24:      }
  25:      #endregion
  26:  }

Hope it is useful to someone. Enjoy

13 Comments so far »

  1. Martin said

    am January 31 2009 @ 4:54 pm

    Sorry, if this is a dumb question, but why a extension method?
    Or better, why didn’t you define the extension method on the Dispatcher type?

  2. Simon said

    am January 31 2009 @ 6:41 pm

    I like the InvokeIfRequired extension method, but a few questions:

    - is there a reason why you are using | and not || ?
    - in MoveItem, you are accessing Count multiple times without locking, isn’t it possible to break that check?

  3. sacha said

    am February 1 2009 @ 8:09 am

    Martin

    Actually looking at that, it would have been better to define it as an Extension method on the dispatcher. And I like extension methods as they are reusable else where. On anything of that type, granted in this case it it pretty limited.

  4. sacha said

    am February 1 2009 @ 8:14 am

    Martin, Actually yeah I think I need to alter that code for MoveItem. Hand on Ill ammend that.

  5. sacha said

    am February 1 2009 @ 8:20 am

    Martin thanks for your very valid comments, I have ammended the post as you suggested.

    Thanks

  6. Martin said

    am February 2 2009 @ 10:32 am

    Your very welcome!

    But the MoveItem comment was by Simon and not by me :)

  7. Aaron Olds said

    am February 2 2009 @ 2:48 pm

    This post is exactly why I check your blog every day for new posts. I was working on a similar solution and now you just saved me a ton of time.

    Thanks!!

  8. wekempf said

    am February 2 2009 @ 5:39 pm

    Have to admit, I don’t get this code.

    If you’re dispatching operations via the Dispatcher, all of the code will executing on a single thread and there should be no need for locking, or did I miss something?

    Finally, maybe I just have a different take on things, but I don’t think it’s ever really appropriate to provide an internally synchronized collection. You get a false sense of security at worst and a performance hit at best. The collection is synchronized only at the method level (i.e. calls to two methods in a row are not synchronized) while what you almost always want is a higher level of synchronization (i.e. I want to protect at the data level, not the method level). Collections are low level data types, and as such, I’m always skeptical when they include synchronization concepts. Synchronization belongs at a much higher level.

    I wish MS would have provided an IDispatcherObject interface instead of just a DispatcherObject abstract class. Then you could have implemented this interface, and left “synchronization” up to the consumer.

  9. sacha said

    am February 2 2009 @ 5:53 pm

    Aaron glad it helps you.

  10. sacha said

    am February 2 2009 @ 5:58 pm

    Bill

    I do agree with what you say, and I was using this as a collection that is visible via a constantly updating source and also a Dispatcher based object (a control) so this is why there is a need to use the Dispatcher, as sometimes the calls are not forced to be invoked on the Dispatcher. See the extension method.

    I know what you mean though, if all calls do need to be invoked to Dispatcher.Currents Thread, its all Single threaded.

    I guess this post made more sense in the context of the problem from wence it came.

    But I feel it still has a place, so I am leaving it here. But thanks for your comments as alawys most enlightening.

  11. Joep Beusenberg said

    am February 23 2009 @ 9:20 pm

    Why not just forget about all those overloads and only overload the OnCollectionChanged and OnPropertyChanged?

    Since all the objects that have a problem getting multithreaded input can only get triggered through those two events, all other processing can be done multithreaded.

    Great topic though!

    Joep

  12. sacha said

    am February 23 2009 @ 9:48 pm

    Joep,

    Yeah that could actually work,Nice one

  13. Kevin said

    am April 2 2009 @ 5:21 pm

    Something interesting… if you were to ignore the ToSyncArray() method… there is no reason even use a lock, since all execution will be on the UI thread :)

Comment RSS · TrackBack URI

Leave a comment

Name: (Required)

eMail: (Required)

Website:

Comment: