Frustrating error in WPF (.NET 4.0) internals: Has

2019-09-01 03:17发布

I have built custom Rules Engine, where each element of this engine is a descendant of DependencyObject class, so that it could be described in XAML including support for dependency properties, markup extensions, bindings, and so on. The purpose of this Rules Engine is to process certain information which comes as an input and to return the output information back to the queue. The entire tree of object is being instantiated once (from XAML) in a single thread and the series of Input objects are being processed from the Input queue. All works fine until I increase the number of threads - sometimes (not always!) it throws the following exception:

    System.InvalidOperationException: Hashtable insert failed. Load factor too high. The most common cause is multiple threads writing to the Hashtable simultaneously.
       at System.Collections.Hashtable.Insert(Object key, Object nvalue, Boolean add)
       at MS.Internal.Data.ValueChangedEventManager.PrivateAddListener(Object source, IWeakEventListener listener, PropertyDescriptor pd)
       at MS.Internal.Data.PropertyPathWorker.ReplaceItem(Int32 k, Object newO, Object parent)
       at MS.Internal.Data.PropertyPathWorker.UpdateSourceValueState(Int32 k, ICollectionView collectionView, Object newValue, Boolean isASubPropertyChange)
       at MS.Internal.Data.PropertyPathWorker.OnDependencyPropertyChanged(DependencyObject d, DependencyProperty dp, Boolean isASubPropertyChange)
       at System.Windows.Data.BindingExpression.HandlePropertyInvalidation(DependencyObject d, DependencyPropertyChangedEventArgs args)
       at System.Windows.Data.BindingExpressionBase.OnPropertyInvalidation(DependencyObject d, DependencyPropertyChangedEventArgs args)
       at System.Windows.Data.BindingExpression.OnPropertyInvalidation(DependencyObject d, DependencyPropertyChangedEventArgs args)
       at System.Windows.DependentList.InvalidateDependents(DependencyObject source, DependencyPropertyChangedEventArgs sourceArgs)
       at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
       at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
       at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
       at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
...

So I wonder, is this a bug in WPF internals or I have to take a closer look at my code? Has anyone experienced such problem before? Since I don't use any global thread-unsafe objects that may interfere with each other, I expect DependencyObject to work correctly in multi-threaded environment, correct?

1条回答
Anthone
2楼-- · 2019-09-01 03:34

Try using a lock() statement around every write to make sure that your writes are not happening simultaneously.

查看更多
登录 后发表回答