WPF密集的UI处理导致ContextSwitchDeadlock
本文关键字:ContextSwitchDeadlock 处理 UI WPF | 更新日期: 2023-09-27 17:49:25
我有一个WPF图表应用程序。基本上,用户从文件中输入数据,应用程序绘制数据图表。当图表中的点数变得非常大时,我得到一个ContextSwitchDeadlock错误,显示
The CLR has been unable to transition from COM context 0x23cda8 to COM context 0x23cf18 for 60 seconds. The thread that owns the destination context/apartment is most likely either doing a non pumping wait or processing a very long running operation without pumping Windows messages. This situation generally has a negative performance impact and may even lead to the application becoming non responsive or memory usage accumulating continually over time. To avoid this problem, all single threaded apartment (STA) threads should use pumping wait primitives (such as CoWaitForMultipleHandles) and routinely pump messages during long running operations.
现在,我意识到这是因为UI线程不应该那么长时间忙碌,但我不清楚我的选择是什么,因为整个时间都花在向UI添加组件上。如果我不断访问UI元素,我就不能使用BackGroundWorker
。那么我该怎么做呢?
有这么多视觉效果真的那么实用吗?如果是这样的话,我会将视觉效果的添加分解,并在单独的信息中进行添加。换句话说,使用Dispatcher对具有适当DispatcherPriority的消息进行排队。每条消息将负责从总数中添加一小块视觉效果。