blob: 317d0e249672878eb6167220bb525fd04c13656f (
plain)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using System.Windows.Threading;
namespace Tango.Core
{
public class SynchronizedObservableCollection<T> : ObservableCollection<T>
{
private object _sync_lock = new object();
private object _modify_lock = new object();
private SynchronizedObservableCollectionDispatcher _dispatcher;
public SynchronizedObservableCollection() : base()
{
_dispatcher = new SynchronizedObservableCollectionDispatcher();
BindingOperations.EnableCollectionSynchronization(this, _modify_lock);
}
public SynchronizedObservableCollection(IEnumerable<T> collection) : base(collection)
{
_dispatcher = new SynchronizedObservableCollectionDispatcher();
BindingOperations.EnableCollectionSynchronization(this, _modify_lock);
}
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
lock (_modify_lock)
{
_dispatcher.Invoke(() =>
{
base.OnCollectionChanged(e);
});
}
}
protected override void InsertItem(int index, T item)
{
lock (_modify_lock)
{
base.InsertItem(index, item);
}
}
protected override void RemoveItem(int index)
{
lock (_modify_lock)
{
base.RemoveItem(index);
}
}
protected override void ClearItems()
{
lock (_modify_lock)
{
base.ClearItems();
}
}
}
internal class SynchronizedObservableCollectionDispatcher
{
internal Dispatcher Dispatcher { get; set; }
internal SynchronizedObservableCollectionDispatcher()
{
if (Application.Current != null)
{
Dispatcher = Application.Current.Dispatcher;
}
}
internal void Invoke(Action action)
{
if (Dispatcher != null)
{
Dispatcher.Invoke(action);
}
else
{
action();
}
}
}
}
|