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