@@ -68,6 +68,7 @@ public interface INoisyCollection : IList, INotifyPropertyChanged, INotifyCollec
68
68
public class NoisyCollection < T > : INoisyCollection , IList < T >
69
69
{
70
70
#region Private Fields
71
+ private readonly object _sync = new object ( ) ;
71
72
private readonly List < T > _source ;
72
73
private const string CountString = "Count" ;
73
74
private const string IndexerString = "Item[]" ;
@@ -153,11 +154,20 @@ event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged
153
154
/// <returns></returns>
154
155
public T this [ int index ]
155
156
{
156
- get { return _source [ index ] ; }
157
+ get
158
+ {
159
+ lock ( _sync )
160
+ {
161
+ return _source [ index ] ;
162
+ }
163
+ }
157
164
set
158
165
{
159
166
var original = this [ index ] ;
160
- _source [ index ] = value ;
167
+ lock ( _sync )
168
+ {
169
+ _source [ index ] = value ;
170
+ }
161
171
ReplaceItem ( original , value , index ) ;
162
172
}
163
173
}
@@ -169,11 +179,20 @@ public T this[int index]
169
179
/// <returns></returns>
170
180
object IList . this [ int index ]
171
181
{
172
- get { return _source [ index ] ; }
182
+ get
183
+ {
184
+ lock ( _sync )
185
+ {
186
+ return _source [ index ] ;
187
+ }
188
+ }
173
189
set
174
190
{
175
191
var original = this [ index ] ;
176
- _source [ index ] = ( T ) value ;
192
+ lock ( _sync )
193
+ {
194
+ _source [ index ] = ( T ) value ;
195
+ }
177
196
ReplaceItem ( original , value , index ) ;
178
197
}
179
198
}
@@ -184,7 +203,10 @@ object IList.this[int index]
184
203
/// <returns>collection enumeration</returns>
185
204
public IEnumerator < T > GetEnumerator ( )
186
205
{
187
- return _source . GetEnumerator ( ) ;
206
+ lock ( _sync )
207
+ {
208
+ return new List < T > ( _source ) . GetEnumerator ( ) ;
209
+ }
188
210
}
189
211
190
212
/// <summary>
@@ -257,7 +279,10 @@ int IList.Add(object value)
257
279
{
258
280
var v = ( T ) value ;
259
281
Add ( v ) ;
260
- return _source . IndexOf ( v ) ;
282
+ lock ( _sync )
283
+ {
284
+ return _source . IndexOf ( v ) ;
285
+ }
261
286
}
262
287
263
288
/// <summary>
@@ -266,7 +291,10 @@ int IList.Add(object value)
266
291
/// <returns>number of items in the collection</returns>
267
292
public void Add ( T item )
268
293
{
269
- _source . Add ( item ) ;
294
+ lock ( _sync )
295
+ {
296
+ _source . Add ( item ) ;
297
+ }
270
298
OnNoisyCollectionChanged ( null , new [ ] { item } ) ;
271
299
OnPropertyChanged ( CountString ) ;
272
300
OnPropertyChanged ( IndexerString ) ;
@@ -290,7 +318,10 @@ public void AddRange(IEnumerable<object> items)
290
318
public void AddRange ( IEnumerable < T > items )
291
319
{
292
320
var newItems = items as T [ ] ?? items . ToArray ( ) ;
293
- _source . AddRange ( newItems ) ;
321
+ lock ( _sync )
322
+ {
323
+ _source . AddRange ( newItems ) ;
324
+ }
294
325
OnNoisyCollectionChanged ( null , newItems ) ;
295
326
OnPropertyChanged ( CountString ) ;
296
327
OnPropertyChanged ( IndexerString ) ;
@@ -307,7 +338,10 @@ public void AddRange(IEnumerable<T> items)
307
338
/// <param name="item">item to insert</param>
308
339
public void Insert ( int index , T item )
309
340
{
310
- _source . Insert ( index , item ) ;
341
+ lock ( _sync )
342
+ {
343
+ _source . Insert ( index , item ) ;
344
+ }
311
345
OnNoisyCollectionChanged ( null , new [ ] { item } ) ;
312
346
OnPropertyChanged ( CountString ) ;
313
347
OnPropertyChanged ( IndexerString ) ;
@@ -343,7 +377,10 @@ public void InsertRange(int index, IEnumerable<object> collection)
343
377
public void InsertRange ( int index , IEnumerable < T > collection )
344
378
{
345
379
var newItems = collection as T [ ] ?? collection . ToArray ( ) ;
346
- _source . InsertRange ( index , newItems ) ;
380
+ lock ( _sync )
381
+ {
382
+ _source . InsertRange ( index , newItems ) ;
383
+ }
347
384
OnNoisyCollectionChanged ( null , newItems ) ;
348
385
OnPropertyChanged ( CountString ) ;
349
386
OnPropertyChanged ( IndexerString ) ;
@@ -369,7 +406,11 @@ public void Remove(object value)
369
406
/// <returns>number of items in the collection</returns>
370
407
public bool Remove ( T item )
371
408
{
372
- var index = _source . IndexOf ( item ) ;
409
+ int index ;
410
+ lock ( _sync )
411
+ {
412
+ index = _source . IndexOf ( item ) ;
413
+ }
373
414
if ( index < 0 ) return false ;
374
415
RemoveAt ( index ) ;
375
416
return true ;
@@ -399,8 +440,12 @@ void IList<T>.RemoveAt(int index)
399
440
/// <param name="index">index to remove at</param>
400
441
public void RemoveAt ( int index )
401
442
{
402
- var item = _source [ index ] ;
403
- _source . RemoveAt ( index ) ;
443
+ T item ;
444
+ lock ( _sync )
445
+ {
446
+ item = _source [ index ] ;
447
+ _source . RemoveAt ( index ) ;
448
+ }
404
449
OnNoisyCollectionChanged ( new [ ] { item } , null ) ;
405
450
OnPropertyChanged ( CountString ) ;
406
451
OnPropertyChanged ( IndexerString ) ;
@@ -429,8 +474,12 @@ void ICollection<T>.Clear()
429
474
/// </summary>
430
475
public void Clear ( )
431
476
{
432
- var backup = _source . ToArray ( ) ;
433
- _source . Clear ( ) ;
477
+ T [ ] backup ;
478
+ lock ( _sync )
479
+ {
480
+ backup = _source . ToArray ( ) ;
481
+ _source . Clear ( ) ;
482
+ }
434
483
OnNoisyCollectionChanged ( backup , null ) ;
435
484
OnNoisyCollectionReset ( ) ;
436
485
OnPropertyChanged ( CountString ) ;
@@ -455,7 +504,10 @@ public bool Contains(object value)
455
504
/// <returns>evaluation</returns>
456
505
public bool Contains ( T item )
457
506
{
458
- return _source . Contains ( item ) ;
507
+ lock ( _sync )
508
+ {
509
+ return _source . Contains ( item ) ;
510
+ }
459
511
}
460
512
461
513
/// <summary>
@@ -475,7 +527,10 @@ public void CopyTo(Array array, int index)
475
527
/// <param name="index">array index</param>
476
528
public void CopyTo ( T [ ] array , int index )
477
529
{
478
- _source . CopyTo ( array , index ) ;
530
+ lock ( _sync )
531
+ {
532
+ _source . CopyTo ( array , index ) ;
533
+ }
479
534
}
480
535
481
536
/// <summary>
@@ -495,7 +550,10 @@ public int IndexOf(object value)
495
550
/// <returns></returns>
496
551
public int IndexOf ( T item )
497
552
{
498
- return _source . IndexOf ( item ) ;
553
+ lock ( _sync )
554
+ {
555
+ return _source . IndexOf ( item ) ;
556
+ }
499
557
}
500
558
501
559
#endregion
0 commit comments