我遇到一个奇怪的问题,偶尔我的WPF应用程序在BindingList<> .OnListChanged()调用中死锁(永远阻塞)。
我创建了自己的类,BindingList的孩子
public class SortableBindingList<tItem> : BindingList<tItem>
{
//I provide an implementation of ApplySortCore so that the list can be sorted
protected override void ApplySortCore(PropertyDescriptor aProp,
ListSortDirection aDirection)
{
//
// I perform sorting here by copying items into a different list, sorting
// and copying back: this[index] = (tItem)(sortedList[index])
//
// Raise the ListChanged event so bound controls refresh their
// values.
OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
}
}
问题发生在OnListChanged()内部。 UI线程永远等待某些东西。
WindowsBase.dll!System.Windows.Threading.DispatcherSynchronizationContext.Wait(System.IntPtr[] waitHandles, bool waitAll, int millisecondsTimeout) Unknown
mscorlib.dll!System.Threading.SynchronizationContext.InvokeWaitMethodHelper(System.Threading.SynchronizationContext syncContext, System.IntPtr[] waitHandles, bool waitAll, int millisecondsTimeout) Unknown
[Native to Managed Transition]
PresentationFramework.dll!System.Windows.SystemResources.FindResourceInternal(object key, bool allowDeferredResourceReference, bool mustReturnDeferredResourceReference) Unknown
PresentationFramework.dll!System.Windows.FrameworkElement.FindResourceInternal(System.Windows.FrameworkElement fe, System.Windows.FrameworkContentElement fce, System.Windows.DependencyProperty dp, object resourceKey, object unlinkedParent, bool allowDeferredResourceReference, bool mustReturnDeferredResourceReference, System.Windows.DependencyObject boundaryElement, bool isImplicitStyleLookup, out object source) Unknown
PresentationFramework.dll!System.Windows.StyleHelper.GetChildValueHelper(System.Windows.UncommonField<System.Collections.Specialized.HybridDictionary[]> dataField, ref MS.Utility.ItemStructList<System.Windows.ChildValueLookup> valueLookupList, System.Windows.DependencyProperty dp, System.Windows.DependencyObject container, MS.Internal.FrameworkObject child, int childIndex, bool styleLookup, ref System.Windows.EffectiveValueEntry entry, out System.Windows.ValueLookupType sourceType, System.Windows.FrameworkElementFactory templateRoot) Unknown
PresentationFramework.dll!System.Windows.StyleHelper.GetChildValue(System.Windows.UncommonField<System.Collections.Specialized.HybridDictionary[]> dataField, System.Windows.DependencyObject container, int childIndex, MS.Internal.FrameworkObject child, System.Windows.DependencyProperty dp, ref MS.Utility.FrugalStructList<System.Windows.ChildRecord> childRecordFromChildIndex, ref System.Windows.EffectiveValueEntry entry, out System.Windows.ValueLookupType sourceType, System.Windows.FrameworkElementFactory templateRoot) Unknown
PresentationFramework.dll!System.Windows.StyleHelper.GetValueFromStyleOrTemplate(MS.Internal.FrameworkObject fo, System.Windows.DependencyProperty dp, ref System.Windows.EffectiveValueEntry entry) Unknown
PresentationFramework.dll!System.Windows.FrameworkElement.GetRawValue(System.Windows.DependencyProperty dp, System.Windows.PropertyMetadata metadata, ref System.Windows.EffectiveValueEntry entry) Unknown
PresentationFramework.dll!System.Windows.FrameworkElement.EvaluateBaseValueCore(System.Windows.DependencyProperty dp, System.Windows.PropertyMetadata metadata, ref System.Windows.EffectiveValueEntry newEntry) Unknown
WindowsBase.dll!System.Windows.DependencyObject.EvaluateEffectiveValue(System.Windows.EntryIndex entryIndex, System.Windows.DependencyProperty dp, System.Windows.PropertyMetadata metadata, System.Windows.EffectiveValueEntry oldEntry, System.Windows.EffectiveValueEntry newEntry, System.Windows.OperationType operationType) Unknown
WindowsBase.dll!System.Windows.DependencyObject.UpdateEffectiveValue(System.Windows.EntryIndex entryIndex, System.Windows.DependencyProperty dp, System.Windows.PropertyMetadata metadata, System.Windows.EffectiveValueEntry oldEntry, ref System.Windows.EffectiveValueEntry newEntry, bool coerceWithDeferredReference, bool coerceWithCurrentValue, System.Windows.OperationType operationType) Unknown
WindowsBase.dll!System.Windows.DependencyObject.InvalidateProperty(System.Windows.DependencyProperty dp, bool preserveCurrentValue) Unknown
PresentationFramework.dll!System.Windows.StyleHelper.InvalidateResourceDependents(System.Windows.DependencyObject container, System.Windows.ResourcesChangeInfo info, ref MS.Utility.FrugalStructList<System.Windows.ChildPropertyDependent> resourceDependents, bool invalidateVisualTreeToo) Unknown
PresentationFramework.dll!System.Windows.TreeWalkHelper.InvalidateStyleAndReferences(System.Windows.DependencyObject d, System.Windows.ResourcesChangeInfo info, bool containsTypeOfKey) Unknown
PresentationFramework.dll!System.Windows.TreeWalkHelper.OnResourcesChanged(System.Windows.DependencyObject d, System.Windows.ResourcesChangeInfo info, bool raiseResourceChangedEvent) Unknown
PresentationFramework.dll!System.Windows.FrameworkElement.OnAncestorChangedInternal(System.Windows.TreeChangeInfo parentTreeState) Unknown
PresentationFramework.dll!System.Windows.TreeWalkHelper.OnAncestorChanged(System.Windows.DependencyObject d, System.Windows.TreeChangeInfo info, bool visitedViaVisualTree) Unknown
PresentationFramework.dll!System.Windows.DescendentsWalker<System.Windows.TreeChangeInfo>.StartWalk(System.Windows.DependencyObject startNode, bool skipStartNode) Unknown
PresentationFramework.dll!MS.Internal.PrePostDescendentsWalker<System.Windows.TreeChangeInfo>.StartWalk(System.Windows.DependencyObject startNode, bool skipStartNode) Unknown
PresentationFramework.dll!System.Windows.TreeWalkHelper.InvalidateOnTreeChange(System.Windows.FrameworkElement fe, System.Windows.FrameworkContentElement fce, System.Windows.DependencyObject parent, bool isAddOperation) Unknown
PresentationFramework.dll!System.Windows.FrameworkElement.OnVisualParentChanged(System.Windows.DependencyObject oldParent) Unknown
PresentationCore.dll!System.Windows.Media.Visual.FireOnVisualParentChanged(System.Windows.DependencyObject oldParent) Unknown
PresentationCore.dll!System.Windows.Media.Visual.RemoveVisualChild(System.Windows.Media.Visual child) Unknown
PresentationCore.dll!System.Windows.Media.VisualCollection.DisconnectChild(int index) Unknown
PresentationCore.dll!System.Windows.Media.VisualCollection.Clear() Unknown
PresentationFramework.dll!System.Windows.Controls.UIElementCollection.ClearInternal() Unknown
PresentationFramework.dll!System.Windows.Controls.Panel.ClearChildren() Unknown
PresentationFramework.dll!System.Windows.Controls.Panel.EnsureEmptyChildren(System.Windows.FrameworkElement logicalParent) Unknown
PresentationFramework.dll!System.Windows.Controls.Panel.OnItemsChangedInternal(object sender, System.Windows.Controls.Primitives.ItemsChangedEventArgs args) Unknown
PresentationFramework.dll!System.Windows.Controls.VirtualizingPanel.OnItemsChangedInternal(object sender, System.Windows.Controls.Primitives.ItemsChangedEventArgs args) Unknown
PresentationFramework.dll!System.Windows.Controls.Panel.OnItemsChanged(object sender, System.Windows.Controls.Primitives.ItemsChangedEventArgs args) Unknown
PresentationFramework.dll!System.Windows.Controls.ItemContainerGenerator.OnRefresh() Unknown
PresentationFramework.dll!System.Windows.Controls.ItemContainerGenerator.OnCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs args) Unknown
WindowsBase.dll!System.Windows.WeakEventManager.ListenerList<System.Collections.Specialized.NotifyCollectionChangedEventArgs>.DeliverEvent(object sender, System.EventArgs e, System.Type managerType) Unknown
WindowsBase.dll!System.Windows.WeakEventManager.DeliverEvent(object sender, System.EventArgs args) Unknown
WindowsBase.dll!System.Collections.Specialized.CollectionChangedEventManager.OnCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs args) Unknown
PresentationFramework.dll!System.Windows.Data.CollectionView.OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs args) Unknown
PresentationFramework.dll!System.Windows.Controls.ItemCollection.OnViewCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) Unknown
WindowsBase.dll!System.Windows.WeakEventManager.ListenerList<System.Collections.Specialized.NotifyCollectionChangedEventArgs>.DeliverEvent(object sender, System.EventArgs e, System.Type managerType) Unknown
WindowsBase.dll!System.Windows.WeakEventManager.DeliverEvent(object sender, System.EventArgs args) Unknown
WindowsBase.dll!System.Collections.Specialized.CollectionChangedEventManager.OnCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs args) Unknown
PresentationFramework.dll!System.Windows.Data.CollectionView.OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs args) Unknown
PresentationFramework.dll!System.Windows.Data.BindingListCollectionView.RefreshOverride() Unknown
PresentationFramework.dll!System.Windows.Data.CollectionView.RefreshInternal() Unknown
PresentationFramework.dll!System.Windows.Data.CollectionView.RefreshOrDefer() Unknown
PresentationFramework.dll!System.Windows.Data.BindingListCollectionView.OnListChanged(object sender, System.ComponentModel.ListChangedEventArgs args) Unknown
System.dll!System.ComponentModel.BindingList<System.__Canon>.OnListChanged(System.ComponentModel.ListChangedEventArgs e) Unknown
我无法弄清楚为什么会阻止它。我只在UI线程上访问此列表,而不是任何其他线程。
非常感谢您提供任何信息。
答案 0 :(得分:0)
我认为你不需要这样做
// Raise the ListChanged event so bound controls refresh their
// values.
OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
删除那个,BindingList
。用户界面应该收听排序,搜索,添加等