ThreadSafeObservableCollection

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:  }

.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

It relies on this small extension method

 

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }

   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:  }

.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }Hope it is useful to someone. Enjoy

About these ads

20 thoughts on “ThreadSafeObservableCollection

  1. 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. 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 says:

    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. Aaron Olds says:

    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!!

  5. 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.

  6. sacha says:

    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.

  7. Joep Beusenberg says:

    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

  8. 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 :)

  9. nayato says:

    Actually this implementation isn’t thread-safe. simple test that should point out the flaw:
    when there are items in collection, call Clear and Add immediately after that. It can end up getting wrong Count in Add method (which you cannot override unfortunately) and end up inserting at with index higher than count of items in the collection => causing error. looks like that if you want thread safe observable collection, you need to write one from the ground..

  10. Sascha Hennig says:

    And another time you save my behind. Whilst this is not so much because of your custom ObservableCollection but mostly due to the link in your last reply, I also really can do with that extension method of yours, so Im gonna nick it xD Thanks – again :)

  11. wathek says:

    I got an error when i tried to initialize the dispatcher (_dispatcher = Dispatcher.CurrentDispatcher;), the visual studio cannot find the member CurrentDispatcher. I googled many times to solve this problem but didn’t find a solution.
    I’m developping a windows phone application and using visual studio professional 2010.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s