Click here to Skip to main content
15,885,875 members
Articles / .NET

ThreadSafeObservableCollection

Rate me:
Please Sign up or sign in to vote.
3.00/5 (3 votes)
17 Jun 2009CPOL 27.4K   11   5
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.

C#
  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:

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

 

License

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


Written By
Software Developer (Senior)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.

Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2016
  • Codeproject MVP 2016
  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

Comments and Discussions

 
GeneralMy vote of 1 Pin
jaruifer23-Dec-09 4:35
jaruifer23-Dec-09 4:35 
GeneralThanks for sharing Pin
Bishoy Demian22-Jun-09 22:38
Bishoy Demian22-Jun-09 22:38 
GeneralRe: Thanks for sharing Pin
Sacha Barber22-Jun-09 23:40
Sacha Barber22-Jun-09 23:40 
Generalcouple of suggestions/questions Pin
Itay Sagui17-Jun-09 4:34
Itay Sagui17-Jun-09 4:34 
GeneralRe: couple of suggestions/questions Pin
Sacha Barber17-Jun-09 8:47
Sacha Barber17-Jun-09 8:47 

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.