forked from ArduPilot/MissionPlanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObservableCollectionThreadSafe.cs
47 lines (44 loc) · 1.36 KB
/
ObservableCollectionThreadSafe.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System;
namespace GMap.NET.ObjectModel
{
public class ObservableCollectionThreadSafe<T> : ObservableCollection<T>
{
NotifyCollectionChangedEventHandler collectionChanged;
public override event NotifyCollectionChangedEventHandler CollectionChanged
{
add
{
collectionChanged += value;
}
remove
{
collectionChanged -= value;
}
}
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
// Be nice - use BlockReentrancy like MSDN said
using(BlockReentrancy())
{
if(collectionChanged != null)
{
Delegate[] delegates = collectionChanged.GetInvocationList();
// Walk thru invocation list
foreach(NotifyCollectionChangedEventHandler handler in delegates)
{
// If the subscriber is a DispatcherObject and different thread
if(handler != null)
{
// Invoke handler in the target dispatcher's thread
handler.Invoke(handler, e);
}
else // Execute handler as is
{
collectionChanged(this, e);
}
}
}
}
}
}
}