我构建了自定义规则引擎,其中此引擎的每个元素都是DependencyObject
类的后代,因此可以在XAML中对其进行描述,包括对依赖项属性,标记扩展,绑定等的支持。
此规则引擎的目的是处理作为输入提供的某些信息并将输出信息返回到队列。
整个对象树在一个线程中被实例化一次(来自XAML),并且正在从Input队列处理一系列Input对象。
一切正常,直到我增加线程数 - 有时(并非总是!)它会引发以下异常:
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)
...
所以我想知道,这是WPF内部的错误还是我需要仔细查看我的代码?以前有没有人遇到这样的问题? 由于我不使用任何可能互相干扰的全局线程不安全对象,我希望DependencyObject在多线程环境中正常工作,对吗?
答案 0 :(得分:0)
尝试在每次写入时使用lock()语句,以确保您的写入不会同时发生。