using System; using System.Collections.Specialized; using System.Windows.Threading; namespace JD.Core { public class MultiThreadedObservableRangeCollection : ObservableRangeCollection { public override event NotifyCollectionChangedEventHandler CollectionChanged; protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e) { var CollectionChanged = this.CollectionChanged; if (CollectionChanged != null) { foreach (NotifyCollectionChangedEventHandler nh in CollectionChanged.GetInvocationList()) { var dispObj = nh.Target as DispatcherObject; if (dispObj != null) { var dispatcher = dispObj.Dispatcher; if (dispatcher != null && !dispatcher.CheckAccess()) { dispatcher.BeginInvoke( (Action)(() => nh.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset))), DispatcherPriority.DataBind); continue; } } nh.Invoke(this, e); } } } } }