JD2022-TU1/main/tools/framework/JD.Core/MultiThreadedObservableRangeCollection.cs

35 lines
1.4 KiB
C#

using System;
using System.Collections.Specialized;
using System.Windows.Threading;
namespace JD.Core
{
public class MultiThreadedObservableRangeCollection<T> : ObservableRangeCollection<T>
{
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);
}
}
}
}
}