C# 在AsyncObservableCollection上实现RemoveAll
我正在尝试在C# 在AsyncObservableCollection上实现RemoveAll,c#,multithreading,C#,Multithreading,我正在尝试在AsyncObservableCollection上实现RemoveAll,创建该类是为了在另一个线程上编辑集合,结构如下: public class AsyncObservableCollection<T> : ObservableCollection<T> { private readonly SynchronizationContext _synchronizationContext = SynchronizationContext.Curren
AsyncObservableCollection
上实现RemoveAll
,创建该类是为了在另一个线程上编辑集合,结构如下:
public class AsyncObservableCollection<T> : ObservableCollection<T>
{
private readonly SynchronizationContext _synchronizationContext = SynchronizationContext.Current;
public AsyncObservableCollection()
{
}
public AsyncObservableCollection(IEnumerable<T> list)
: base(list)
{
}
private void ExecuteOnSyncContext(Action action)
{
if (SynchronizationContext.Current == _synchronizationContext)
{
action();
}
else
{
_synchronizationContext.Send(_ => action(), null);
}
}
protected override void InsertItem(int index, T item)
{
ExecuteOnSyncContext(() => base.InsertItem(index, item));
}
protected override void RemoveItem(int index)
{
ExecuteOnSyncContext(() => base.RemoveItem(index));
}
protected override void SetItem(int index, T item)
{
ExecuteOnSyncContext(() => base.SetItem(index, item));
}
protected override void MoveItem(int oldIndex, int newIndex)
{
ExecuteOnSyncContext(() => base.MoveItem(oldIndex, newIndex));
}
protected override void ClearItems()
{
ExecuteOnSyncContext(() => base.ClearItems());
}
public void RemoveAll(Predicate<T> predicate)
{
CheckReentrancy();
List<T> itemsToRemove = Items.Where(x => predicate(x)).ToList();
itemsToRemove.ForEach(item => Items.Remove(item));
OnPropertyChanged(new PropertyChangedEventArgs("Count"));
OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
}
我可以做些什么来解决这个问题?我不确定这个类的目的以及为什么需要保持当前的上下文,但无论如何,问题的解决方案是
public class ObservableCollectionAsync<T> : ObservableCollection<T>
{
public ObservableCollectionAsync()
{
BindingOperations.EnableCollectionSynchronization(this, _lock);
}
public ObservableCollectionAsync(List<T> list) : base(list)
{
BindingOperations.EnableCollectionSynchronization(this, _lock);
}
public ObservableCollectionAsync(IEnumerable<T> collection) : base(collection)
{
BindingOperations.EnableCollectionSynchronization(this, _lock);
}
private readonly SynchronizationContext _synchronizationContext = SynchronizationContext.Current;
private readonly object _lock = new object();
private void ExecuteOnSyncContext(Action action)
{
if (SynchronizationContext.Current == _synchronizationContext)
{
action();
}
else
{
_synchronizationContext.Send(_ => action(), null);
}
}
protected override void ClearItems()
{
ExecuteOnSyncContext(() => base.ClearItems());
}
protected override void InsertItem(int index, T item)
{
ExecuteOnSyncContext(() => base.InsertItem(index, item));
}
protected override void MoveItem(int oldIndex, int newIndex)
{
ExecuteOnSyncContext(() => base.MoveItem(oldIndex, newIndex));
}
protected override void RemoveItem(int index)
{
ExecuteOnSyncContext(() => base.RemoveItem(index));
}
protected override void SetItem(int index, T item)
{
ExecuteOnSyncContext(() => base.SetItem(index, item));
}
public void RemoveAll(Func<T,bool> predicate)
{
CheckReentrancy();
foreach (var item in this.Where(predicate).ToArray())
{
this.Remove(item);
}
}
公共类ObservableCollectionAsync:ObservableCollection
{
公共ObservableCollectionAsync()
{
BindingOperations.EnableCollectionSynchronization(此锁);
}
公共ObservableCollectionAsync(列表):基本(列表)
{
BindingOperations.EnableCollectionSynchronization(此锁);
}
公共ObservableCollectionAsync(IEnumerable集合):基(集合)
{
BindingOperations.EnableCollectionSynchronization(此锁);
}
私有只读同步上下文_SynchronizationContext=SynchronizationContext.Current;
私有只读对象_lock=新对象();
私有void ExecuteOnSyncContext(操作)
{
if(SynchronizationContext.Current==\u SynchronizationContext)
{
动作();
}
其他的
{
_synchronizationContext.Send(=>action(),null);
}
}
受保护的覆盖无效ClearItems()
{
ExecuteOnSyncContext(()=>base.ClearItems());
}
受保护的覆盖无效插入项(int索引,T项)
{
ExecuteOnSyncContext(()=>base.InsertItem(索引,项));
}
受保护的覆盖无效移动项(int-oldIndex、int-newIndex)
{
ExecuteOnSyncContext(()=>base.MoveItem(oldIndex,newIndex));
}
受保护的覆盖void removietem(int索引)
{
ExecuteOnSyncContext(()=>base.removietem(index));
}
受保护的覆盖无效集合项(整数索引,T项)
{
ExecuteOnSyncContext(()=>base.SetItem(index,item));
}
public void RemoveAll(Func谓词)
{
检查可重入性();
foreach(此.Where(谓词).ToArray()中的变量项)
{
此项。删除(项目);
}
}
但Observable集合中的任何其他方法本身都不会经过“ExecuteOnSyncContext”,因此最好将其公开并在类外为其他方法调用
否则,您将需要使用ICollection接口构建一个可观察的集合
public class ObservableCollectionAsync<T> : ObservableCollection<T>
{
public ObservableCollectionAsync()
{
BindingOperations.EnableCollectionSynchronization(this, _lock);
}
public ObservableCollectionAsync(List<T> list) : base(list)
{
BindingOperations.EnableCollectionSynchronization(this, _lock);
}
public ObservableCollectionAsync(IEnumerable<T> collection) : base(collection)
{
BindingOperations.EnableCollectionSynchronization(this, _lock);
}
private readonly SynchronizationContext _synchronizationContext = SynchronizationContext.Current;
private readonly object _lock = new object();
private void ExecuteOnSyncContext(Action action)
{
if (SynchronizationContext.Current == _synchronizationContext)
{
action();
}
else
{
_synchronizationContext.Send(_ => action(), null);
}
}
protected override void ClearItems()
{
ExecuteOnSyncContext(() => base.ClearItems());
}
protected override void InsertItem(int index, T item)
{
ExecuteOnSyncContext(() => base.InsertItem(index, item));
}
protected override void MoveItem(int oldIndex, int newIndex)
{
ExecuteOnSyncContext(() => base.MoveItem(oldIndex, newIndex));
}
protected override void RemoveItem(int index)
{
ExecuteOnSyncContext(() => base.RemoveItem(index));
}
protected override void SetItem(int index, T item)
{
ExecuteOnSyncContext(() => base.SetItem(index, item));
}
public void RemoveAll(Func<T,bool> predicate)
{
CheckReentrancy();
foreach (var item in this.Where(predicate).ToArray())
{
this.Remove(item);
}
}