|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Runtime.Serialization; |
| 4 | +using System.Text; |
| 5 | +using ThunderDesign.Net.Threading.Interfaces; |
| 6 | +using ThunderDesign.Net.Threading.Threading; |
| 7 | + |
| 8 | +namespace ThunderDesign.Net.Threading.Collections |
| 9 | +{ |
| 10 | + public class DictionaryThreadSafe<TKey, TValue> : Dictionary<TKey, TValue>, IDictionaryThreadSafe<TKey, TValue> |
| 11 | + { |
| 12 | + #region constructors |
| 13 | + public DictionaryThreadSafe() : base() { } |
| 14 | + public DictionaryThreadSafe(int capacity) : base(capacity) { } |
| 15 | + public DictionaryThreadSafe(IEqualityComparer<TKey> comparer) : base(comparer) { } |
| 16 | + public DictionaryThreadSafe(IDictionary<TKey, TValue> dictionary) : base(dictionary) { } |
| 17 | + public DictionaryThreadSafe(int capacity, IEqualityComparer<TKey> comparer) : base(capacity, comparer) { } |
| 18 | + public DictionaryThreadSafe(IDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey> comparer) : base(dictionary, comparer) { } |
| 19 | + #endregion |
| 20 | + |
| 21 | + #region properties |
| 22 | + public new IEqualityComparer<TKey> Comparer |
| 23 | + { |
| 24 | + get |
| 25 | + { |
| 26 | + _ReaderWriterNotifyLock.EnterReadLock(); |
| 27 | + try |
| 28 | + { |
| 29 | + return base.Comparer; |
| 30 | + } |
| 31 | + finally |
| 32 | + { |
| 33 | + _ReaderWriterNotifyLock.ExitReadLock(); |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + public new int Count |
| 39 | + { |
| 40 | + get |
| 41 | + { |
| 42 | + _ReaderWriterNotifyLock.EnterReadLock(); |
| 43 | + try |
| 44 | + { |
| 45 | + return base.Count; |
| 46 | + } |
| 47 | + finally |
| 48 | + { |
| 49 | + _ReaderWriterNotifyLock.ExitReadLock(); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + public new KeyCollection Keys |
| 55 | + { |
| 56 | + get |
| 57 | + { |
| 58 | + _ReaderWriterNotifyLock.EnterReadLock(); |
| 59 | + try |
| 60 | + { |
| 61 | + return base.Keys; |
| 62 | + } |
| 63 | + finally |
| 64 | + { |
| 65 | + _ReaderWriterNotifyLock.ExitReadLock(); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + public new ValueCollection Values |
| 71 | + { |
| 72 | + get |
| 73 | + { |
| 74 | + _ReaderWriterNotifyLock.EnterReadLock(); |
| 75 | + try |
| 76 | + { |
| 77 | + return base.Values; |
| 78 | + } |
| 79 | + finally |
| 80 | + { |
| 81 | + _ReaderWriterNotifyLock.ExitReadLock(); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + public new TValue this[TKey key] |
| 87 | + { |
| 88 | + get |
| 89 | + { |
| 90 | + _ReaderWriterNotifyLock.EnterReadLock(); |
| 91 | + try |
| 92 | + { |
| 93 | + return base[key]; |
| 94 | + } |
| 95 | + finally |
| 96 | + { |
| 97 | + _ReaderWriterNotifyLock.ExitReadLock(); |
| 98 | + } |
| 99 | + } |
| 100 | + set |
| 101 | + { |
| 102 | + _ReaderWriterNotifyLock.EnterWriteLock(); |
| 103 | + try |
| 104 | + { |
| 105 | + base[key] = value; |
| 106 | + } |
| 107 | + finally |
| 108 | + { |
| 109 | + _ReaderWriterNotifyLock.ExitWriteLock(); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + #endregion |
| 114 | + |
| 115 | + #region methods |
| 116 | + public new virtual void Add(TKey key, TValue value) |
| 117 | + { |
| 118 | + _ReaderWriterNotifyLock.EnterWriteLock(); |
| 119 | + try |
| 120 | + { |
| 121 | + base.Add(key, value); |
| 122 | + } |
| 123 | + finally |
| 124 | + { |
| 125 | + _ReaderWriterNotifyLock.ExitWriteLock(); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + public new virtual void Clear() |
| 130 | + { |
| 131 | + _ReaderWriterNotifyLock.EnterWriteLock(); |
| 132 | + try |
| 133 | + { |
| 134 | + base.Clear(); |
| 135 | + } |
| 136 | + finally |
| 137 | + { |
| 138 | + _ReaderWriterNotifyLock.ExitWriteLock(); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + public new bool ContainsKey(TKey key) |
| 143 | + { |
| 144 | + _ReaderWriterNotifyLock.EnterReadLock(); |
| 145 | + try |
| 146 | + { |
| 147 | + return base.ContainsKey(key); |
| 148 | + } |
| 149 | + finally |
| 150 | + { |
| 151 | + _ReaderWriterNotifyLock.ExitReadLock(); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + public new bool ContainsValue(TValue value) |
| 156 | + { |
| 157 | + _ReaderWriterNotifyLock.EnterReadLock(); |
| 158 | + try |
| 159 | + { |
| 160 | + return base.ContainsValue(value); |
| 161 | + } |
| 162 | + finally |
| 163 | + { |
| 164 | + _ReaderWriterNotifyLock.ExitReadLock(); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + public new Enumerator GetEnumerator() |
| 169 | + { |
| 170 | + _ReaderWriterNotifyLock.EnterReadLock(); |
| 171 | + try |
| 172 | + { |
| 173 | + return base.GetEnumerator(); |
| 174 | + } |
| 175 | + finally |
| 176 | + { |
| 177 | + _ReaderWriterNotifyLock.ExitReadLock(); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + [System.Security.SecurityCritical] // auto-generated_required |
| 182 | + public override void GetObjectData(SerializationInfo info, StreamingContext context) |
| 183 | + { |
| 184 | + _ReaderWriterNotifyLock.EnterReadLock(); |
| 185 | + try |
| 186 | + { |
| 187 | + base.GetObjectData(info, context); |
| 188 | + } |
| 189 | + finally |
| 190 | + { |
| 191 | + _ReaderWriterNotifyLock.ExitReadLock(); |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + public override void OnDeserialization(Object sender) |
| 196 | + { |
| 197 | + _ReaderWriterNotifyLock.EnterReadLock(); |
| 198 | + try |
| 199 | + { |
| 200 | + base.OnDeserialization(sender); |
| 201 | + } |
| 202 | + finally |
| 203 | + { |
| 204 | + _ReaderWriterNotifyLock.ExitReadLock(); |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + public new virtual bool Remove(TKey key) |
| 209 | + { |
| 210 | + bool result = false; |
| 211 | + _ReaderWriterNotifyLock.EnterWriteLock(); |
| 212 | + try |
| 213 | + { |
| 214 | + result = base.Remove(key); |
| 215 | + } |
| 216 | + finally |
| 217 | + { |
| 218 | + _ReaderWriterNotifyLock.ExitWriteLock(); |
| 219 | + } |
| 220 | + return result; |
| 221 | + } |
| 222 | + |
| 223 | + public new bool TryGetValue(TKey key, out TValue value) |
| 224 | + { |
| 225 | + _ReaderWriterNotifyLock.EnterReadLock(); |
| 226 | + try |
| 227 | + { |
| 228 | + return base.TryGetValue(key, out value); |
| 229 | + } |
| 230 | + finally |
| 231 | + { |
| 232 | + _ReaderWriterNotifyLock.ExitReadLock(); |
| 233 | + } |
| 234 | + } |
| 235 | + #endregion |
| 236 | + |
| 237 | + #region variables |
| 238 | + protected static readonly ReaderWriterNotifyLock _ReaderWriterNotifyLock = new ReaderWriterNotifyLock(); |
| 239 | + |
| 240 | + #endregion |
| 241 | + } |
| 242 | +} |
0 commit comments