【发布时间】:2013-12-27 11:51:45
【问题描述】:
我有一个 WPF 应用程序,其中包含许多支持 INotifyProprtyChange 接口的类。此类上的属性从不同的线程高速更改,问题是在某些情况下应用程序 UI 线程挂起,连同 UI 线程,具有代码调用 PropertyChange 的其他线程在尝试更新属性时也会挂起。
我用 Windbg 进行了死锁检查,但它没有检测到。 现在我查看了持有锁的线程的堆栈跟踪,我总是看到至少一个相同或不同的线程有
00000000002d0ab8 000000007712908a [HelperMethodFrame_1OBJ: 00000000002d0ab8] System.Threading.SynchronizationContext.WaitHelper(IntPtr[], Boolean, Int32)
00000000002d13b0 000000007712908a [GCFrame: 00000000002d13b0]
00000000002d16c8 000000007712908a [HelperMethodFrame_1OBJ: 00000000002d16c8] System.Threading.ReaderWriterLock.AcquireReaderLockInternal(Int32)
00000000002d17f0 000007fef1bcf1a0 MS.Internal.ReaderWriterLockWrapper.get_ReadLock()
00000000002d1830 000007fef1ba8d44 System.ComponentModel.PropertyChangedEventManager.OnPropertyChanged(System.Object, System.ComponentModel.PropertyChangedEventArgs)
还有一个
000000001f64d138 000000007738186a [HelperMethodFrame_1OBJ: 000000001f64d138] System.Threading.ReaderWriterLock.AcquireReaderLockInternal(Int32)
000000001f64d260 000007fef1bcf1a0 MS.Internal.ReaderWriterLockWrapper.get_ReadLock()
000000001f64d2a0 000007fef1ba8d44 System.ComponentModel.PropertyChangedEventManager.OnPropertyChanged(System.Object, System.ComponentModel.PropertyChangedEventArgs)
所以它看起来像 PropertyChangedEventManager 等待某个线程完成而其他线程等待获取锁?
现在我很难弄清楚为什么会发生这种情况,因为它是零星的,并且通常发生在有许多属性更改事件时。从我看到的应用程序中没有调用属性更改的其他线程继续正常运行。
【问题讨论】:
-
您可以编写一个日志文件来查找产生错误的属性。每次 PropertyChanged 时,如果现在出现问题,第一件事应该是将属性名称保存在文件中,列表中的最后一个属性应该是造成问题的那个
-
对 NotifyProprtyChanged 的调用会堆积起来。尝试更改支持变量,然后只需调用 NotifyProprtyChanged 一次。
-
@Blam 好吧,我做了一些更改,但仍然出现问题,但使用了不同类型的代码阻塞。顺便说一句,问题通常发生在分辨率更改时。 [代码] [托管到本机转换] WindowsBase.dll!System.Windows.Threading.DispatcherSynchronizationContext.Wait(System.IntPtr[] waitHandles, bool waitAll, int millisecondsTimeout) 未知 [本机到托管转换] WindowsBase.dll!System.Windows .WeakEventManager.DeliverEvent(object sender = {System.Windows.Interactivity.InvokeCommandAction}, System.EventArgs args = {System.EventArgs}) 未知
标签: wpf deadlock inotifypropertychanged