1: public class ConcurrentDictionary<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>
2: {
3: private ReaderWriterLock rw = new ReaderWriterLock();
4: private Dictionary<TKey, TValue> dic = new Dictionary<TKey, TValue>();
5: private int timeout = -1;
6: public void Add(TKey key, TValue value)
7: {
8: rw.AcquireWriterLock(timeout);
9: try
10: {
11: if (!dic.ContainsKey(key))
12: {
13: dic.Add(key, value);
14: }
15: }
16: finally
17: {
18: rw.ReleaseWriterLock();
19: }
20: }
21:
22: public bool ContainsKey(TKey key)
23: {
24: bool res = false;
25: rw.AcquireReaderLock(timeout);
26: try
27: {
28: res = dic.ContainsKey(key);
29: }
30: finally
31: {
32: rw.ReleaseReaderLock();
33: }
34: return res;
35: }
36:
37: public ICollection<TKey> Keys
38: {
39: get
40: {
41: ICollection<TKey> res = null;
42: rw.AcquireReaderLock(timeout);
43: try
44: {
45: Dictionary<TKey, TValue> tmp = new Dictionary<TKey, TValue>(dic);
46: res = tmp.Keys;
47: }
48: finally
49: {
50: rw.ReleaseReaderLock();
51: }
52: return res;
53: }
54: }
55:
56: public bool Remove(TKey key)
57: {
58: bool res = false;
59: rw.AcquireWriterLock(timeout);
60: try
61: {
62: res = dic.Remove(key);
63: }
64: finally
65: {
66: rw.ReleaseWriterLock();
67: }
68: return res;
69: }
70:
71: public bool TryGetValue(TKey key, out TValue value)
72: {
73: bool res = false;
74: rw.AcquireWriterLock(timeout);
75: try
76: {
77: res = dic.TryGetValue(key, out value);
78: }
79: finally
80: {
81: rw.ReleaseWriterLock();
82: }
83: return res;
84: }
85:
86: public ICollection<TValue> Values
87: {
88: get
89: {
90: ICollection<TValue> res = null;
91: rw.AcquireReaderLock(timeout);
92: try
93: {
94: Dictionary<TKey, TValue> tmp = new Dictionary<TKey, TValue>(dic);
95: res = tmp.Values;
96: }
97: finally
98: {
99: rw.ReleaseReaderLock();
100: }
101: return res;
102: }
103: }
104:
105: public TValue this[TKey key]
106: {
107: get
108: {
109: TValue res = default(TValue);
110: rw.AcquireReaderLock(timeout);
111: try
112: {
113: if (dic.ContainsKey(key))
114: {
115: res = dic[key];
116: }
117: }
118: finally
119: {
120: rw.ReleaseWriterLock();
121: }
122: return res;
123: }
124: set
125: {
126: if (ContainsKey(key))
127: {
128: rw.AcquireWriterLock(timeout);
129: try
130: {
131: dic[key] = value;
132: }
133: finally
134: {
135: rw.ReleaseWriterLock();
136: }
137: }
138: }
139: }
140:
141:
142:
143: public void Add(KeyValuePair<TKey, TValue> item)
144: {
145: Add(item.Key, item.Value);
146: }
147:
148: public void Clear()
149: {
150: rw.AcquireWriterLock(timeout);
151: try
152: {
153: dic.Clear();
154: }
155: finally
156: {
157: rw.ReleaseWriterLock();
158: }
159: }
160:
161: public bool Contains(KeyValuePair<TKey, TValue> item)
162: {
163: return ContainsKey(item.Key);
164: }
165:
166: public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
167: {
168: throw new NotImplementedException();
169: }
170:
171: public int Count
172: {
173: get
174: {
175: int count = -1;
176: rw.AcquireReaderLock(timeout);
177: try
178: {
179: count = dic.Count;
180: }
181: finally
182: {
183: rw.ReleaseReaderLock();
184: }
185: return count;
186: }
187: }
188:
189: public bool IsReadOnly
190: {
191: get { return false; }
192: }
193:
194: public bool Remove(KeyValuePair<TKey, TValue> item)
195: {
196: bool res = false;
197: if (ContainsKey(item.Key))
198: {
199: Remove(item.Key);
200: }
201: return res;
202: }
203:
204:
205:
206: public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
207: {
208: IEnumerator<KeyValuePair<TKey, TValue>> res = null;
209: rw.AcquireReaderLock(timeout);
210: try
211: {
212: Dictionary<TKey, TValue> tmp = new Dictionary<TKey, TValue>(dic);
213: res = tmp.GetEnumerator();
214: }
215: finally
216: {
217: rw.ReleaseReaderLock();
218: }
219: return res;
220: }
221:
222:
223:
224: IEnumerator IEnumerable.GetEnumerator()
225: {
226: return null;
227: }
228:
229:
230:
231: IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator()
232: {
233: return null;
234: }
235:
236:
237: }