diff options
| author | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-08-28 10:38:59 +0300 |
|---|---|---|
| committer | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-08-28 10:38:59 +0300 |
| commit | 9785613e7897747231f0e0f1d02c76ce8407e920 (patch) | |
| tree | f2d1b7160ee99d21d920638fcfef22bb6f29991e /Software/Visual_Studio/Tango.Core/SynchronizedObservableCollection.cs | |
| parent | ffcb06ec20597465cdbe62040bacfb4bfc7d5de0 (diff) | |
| download | Tango-9785613e7897747231f0e0f1d02c76ce8407e920.tar.gz Tango-9785613e7897747231f0e0f1d02c76ce8407e920.zip | |
Resolved another threading issue with SynchronizedObservableCollection.
Related Work Items: #229
Diffstat (limited to 'Software/Visual_Studio/Tango.Core/SynchronizedObservableCollection.cs')
| -rw-r--r-- | Software/Visual_Studio/Tango.Core/SynchronizedObservableCollection.cs | 61 |
1 files changed, 55 insertions, 6 deletions
diff --git a/Software/Visual_Studio/Tango.Core/SynchronizedObservableCollection.cs b/Software/Visual_Studio/Tango.Core/SynchronizedObservableCollection.cs index f0a13b3c9..b12c48ded 100644 --- a/Software/Visual_Studio/Tango.Core/SynchronizedObservableCollection.cs +++ b/Software/Visual_Studio/Tango.Core/SynchronizedObservableCollection.cs @@ -6,34 +6,83 @@ 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 static object _sync_lock = new object(); + private static object _modify_lock = new object(); + public SynchronizedObservableCollection() : base() { - this.EnableCrossThreadOperations(); + BindingOperations.EnableCollectionSynchronization(this, _sync_lock); } public SynchronizedObservableCollection(IEnumerable<T> collection) : base(collection) { - + BindingOperations.EnableCollectionSynchronization(this, _sync_lock); } protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e) { - if (ObservableEntityCollectionSettings._dispatcher != null) + SynchronizedObservableCollectionDispatcher.Invoke(() => { - ObservableEntityCollectionSettings._dispatcher.BeginInvoke(new Action(() => + lock (_modify_lock) { 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 static class SynchronizedObservableCollectionDispatcher + { + internal static Dispatcher Dispatcher { get; set; } + + static SynchronizedObservableCollectionDispatcher() + { + if (Application.Current != null) + { + Dispatcher = Application.Current.Dispatcher; + } + } + + internal static void Invoke(Action action) + { + if (Dispatcher != null) + { + Dispatcher.BeginInvoke(action); } else { - base.OnCollectionChanged(e); + action(); } } } |
