标签: cross-thread

创建者更新后,在OnCollectionChanged上获取线程访问异常

在我的程序中,我有一个ObservableKeyedCollection<TKey, TItem>继承自KeyedCollection<TKey, TItem>并实现的抽象类INotifyCollectionChanged.

这个抽象类的实现必然是一个ListBox.在这里ListBox,我在双击时编辑项目,一旦接受,我从该ObservableKeyedCollection<TKey, TItem>实现中删除已编辑项目的旧实例,并添加已修改的新实例.

在Windows 10 Creators Update(1703,内部版本号15063.250)之前,这一切都运行良好.自更新以来,ObservableKeyedCollection<TKey, TItem>开始InvalidOperationException使用以下消息抛出s:

调用线程无法访问此对象,因为另一个线程拥有它.

我不在代码的这个区域中使用任何异步操作.

整个堆栈跟踪太长,但这里是开头的顶部OnCollectionChanged:

在System.Windows.Threading.Dispatcher.VerifyAccess()在System.Windows.Threading.DispatcherObject.VerifyAccess()在System.Windows.DependencyObject.GetValue(的DependencyProperty DP)在System.Windows.Controls.Primitives.Selector.GetIsSelected(DependencyObject的元件)在System.Windows.Controls.Primitives.Selector.ItemSetIsSelected(ItemInfo信息,布尔值)System.Windows.Controls.Primitives.Selector.SelectionChanger.CreateDeltaSelectionChange(List'1 unselectedItems,List'1 selectedItems)在System.Windows .Controls.Primitives.Selector.SelectionChanger.End()在System.Windows.Controls.Primitives.Selector.RemoveFromSelection(NotifyCollectionChangedEventArgs e)上System.Windows.Controls.Primitives.Selector.OnItemsChanged(NotifyCollectionChangedEventArgs e)上System.Windows.Controls .ItemsControl.OnItemCollectionChanged2(对象发件人,NotifyCollectionChangedEventArgs e)上System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(对象发件人,NotifyCollectionChang edEventArgs e)位于System.Windows.WeakE.EventCollection.OnCollectionChanged(Object sender,NotifyCollectionChangedEventArgs e)的System.Windows.Data.CollectionView.OnCollectionChanged(NotifyCollectionChangedEventArgs args),位于System.Windows.WeakEventManager.ListenerList'1.DeliverEvent(Object sender, EventArgs的,类型managerType)在System.Windows.WeakEventManager.DeliverEventToList(对象发件人,EventArgs指定参数时,ListenerList列表)在System.Windows.WeakEventManager.DeliverEvent(对象发件人,EventArgs参数)在System.Collections.Specialized.CollectionChangedEventManager.OnCollectionChanged(对象发件人,NotifyCollectionChangedEventArgs参数)在System.Windows.Data.CollectionView.OnCollectionChanged(NotifyCollectionChangedEventArgs参数)在System.Windows.Data.ListCollectionView.ProcessCollectionChangedWithAdjustedIndex(NotifyCollectionChangedEventArgs指定参数时,的Int32 adjustedOldIndex,的Int32 adjustedNewIndex)在System.Windows.Data.ListCollectionView.ProcessCollectionChanged (通知 CollectionChangedEventArgs args)位于E:\ Phil \Programozás\ Modulok\TetheredSun.1.0\TetheredSun\ObservableKeyedCollection的TetheredSun.ObservableKeyedCollection'2.OnCollectionChanged(NotifyCollectionChangedEventArgs e)的System.Windows.Data.CollectionView.OnCollectionChanged(Object sender,NotifyCollectionChangedEventArgs args). cs,行号:68 at TetheredSun.ObservableKeyedCollection`2.RemoveItem(Int32 index)at [...]

编辑1:

以下是在创建者更新(覆盖KeyedCollection<TKey, TItem>.RemoveItem(int index) )之前可以正常工作的违规代码部分:

protected override void RemoveItem(int index)
{
    TItem item = this[index];
    base.RemoveItem(index); …
Run Code Online (Sandbox Code Playgroud)

c# wpf inotifycollectionchanged cross-thread

5
推荐指数
1
解决办法
528
查看次数

标签 统计

c# ×1

cross-thread ×1

inotifycollectionchanged ×1

wpf ×1