1 | using System.Collections.Generic; |
---|
2 | using System.Collections.ObjectModel; |
---|
3 | using System.Threading; |
---|
4 | using System.Collections.Specialized; |
---|
5 | using System.ComponentModel; |
---|
6 | |
---|
7 | namespace Com.Piwigo.Wpf.DTO |
---|
8 | { |
---|
9 | public class AsyncObservableCollection<T> : ObservableCollection<T> |
---|
10 | { |
---|
11 | |
---|
12 | public AsyncObservableCollection() |
---|
13 | { |
---|
14 | } |
---|
15 | |
---|
16 | public AsyncObservableCollection(IEnumerable<T> list) |
---|
17 | : base(list) |
---|
18 | { |
---|
19 | } |
---|
20 | |
---|
21 | protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e) |
---|
22 | { |
---|
23 | if (SynchronizationContext.Current == PwgModelManager.WPF_SynchronizationContext) |
---|
24 | { |
---|
25 | // Execute the CollectionChanged event on the current thread |
---|
26 | RaiseCollectionChanged(e); |
---|
27 | } |
---|
28 | else |
---|
29 | { |
---|
30 | // Send the CollectionChanged event on the creator thread |
---|
31 | PwgModelManager.WPF_SynchronizationContext.Send(RaiseCollectionChanged, e); |
---|
32 | } |
---|
33 | } |
---|
34 | |
---|
35 | private void RaiseCollectionChanged(object param) |
---|
36 | { |
---|
37 | // We are in the creator thread, call the base implementation directly |
---|
38 | base.OnCollectionChanged((NotifyCollectionChangedEventArgs)param); |
---|
39 | } |
---|
40 | |
---|
41 | protected override void OnPropertyChanged(PropertyChangedEventArgs e) |
---|
42 | { |
---|
43 | if (SynchronizationContext.Current == PwgModelManager.WPF_SynchronizationContext) |
---|
44 | { |
---|
45 | // Execute the PropertyChanged event on the current thread |
---|
46 | RaisePropertyChanged(e); |
---|
47 | } |
---|
48 | else |
---|
49 | { |
---|
50 | // Send the PropertyChanged event on the creator thread |
---|
51 | PwgModelManager.WPF_SynchronizationContext.Send(RaisePropertyChanged, e); |
---|
52 | } |
---|
53 | } |
---|
54 | |
---|
55 | private void RaisePropertyChanged(object param) |
---|
56 | { |
---|
57 | // We are in the creator thread, call the base implementation directly |
---|
58 | base.OnPropertyChanged((PropertyChangedEventArgs)param); |
---|
59 | } |
---|
60 | } |
---|
61 | } |
---|