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: 2: 3: 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: 24: 25: 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: 43: 44: 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: 66: 67: 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: 97: 98: 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: 120: 121: 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: 141: 142: 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: 2: 3: 4: public static class WPFControlThreadingExtensions
5: {
6: #region Public Methods
7: 8: 9: 10: 11: 12: 13: 14: 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!
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