简体   繁体   English

WPF调度程序操作泄漏

[英]WPF Dispatcher Operations Leak

I'm seeing a memory leak in our WPF application. 我在WPF应用程序中看到内存泄漏。 In using .NET Memory Profiler, I can see that even after closing a window, the DataContext and UI elements are all sticking around because of Action delegates that are in DispatcherOperations in the Dispatcher queue...but have a ContextIdle priority and never seem to get processed. 在使用.NET Memory Profiler时,我可以看到,即使在关闭窗口之后,由于Dispatcher队列中DispatcherOperations中的Action委托都存在DataContext和UI元素,但是它们始终处于ContextIdle优先级,而且似乎从未得到处理。

Can I force the Dispatcher queue to flush when my Window gets closed? 当我的窗口关闭时,是否可以强制分派器队列刷新?

You could try to include a synchronous call to a dummy method using dispatcher.Invoke with priority ContextIdle in the Window.Closing event. 您可以尝试使用调度程序包括对虚拟方法的同步调用。在Window.Closing事件中以优先级ContextIdle调用。 This should stop the UI thread from generating more dispatcher items, and should return only after the last ContextIdle items are processed. 这应该阻止UI线程生成更多调度程序项,并且应该仅在处理完最后一个ContextIdle项后返回。 However, I couldn't verify that since without the Telerik component ContextIdle items get processed before the window closes, anyway. 但是,由于没有Telerik组件,无论如何我都无法在窗口关闭之前处理ContextIdle项目。

I could also imagine that those items run in a loop, ie create themselves anew during their execution. 我还可以想象那些项目会循环运行,即在执行过程中重新创建自己。 In that case, it might help to set the DataContext explicitly to null before you close the window. 在这种情况下,关闭窗口之前将DataContext显式设置为null可能会有所帮助。 This might signal to the components that they can stop their loop, and at least you won't leak instances of your datacontext objects this way. 这可能会通知组件它们可以停止循环,并且至少您不会以这种方式泄漏datacontext对象的实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM