“无效的窗口句柄”退出使用 System.AddIn 的 WPF 应用程序时
我正在开发一个 WPF 应用程序,该应用程序使用 System.AddIn 来解决包装在 WindowsFormsHost 控件中的基于 Windows 窗体的控件所遇到的内存泄漏问题。该加载项用于根据需要加载和卸载基于 Windows 窗体的控件,以避免 WindowsFormsHost 的开销,在当前版本的 WPF 中,WindowsFormsHost 会一直挂起,直到应用程序关闭,并且由于以下原因,基于 Windows 窗体的控件中会出现内存泄漏糟糕的清理逻辑。
我们面临的问题是,在应用程序中加载和卸载加载项后,WPF 应用程序将在应用程序退出时抛出“无效窗口句柄”的 Win32 异常。这通常不是一个可怕的问题,但是即使可以捕获异常,它也不能阻止 Windows 将应用程序视为崩溃并在 Windows 7 下显示崩溃对话框,这是不可接受的。
关于代码,相关事实是:
仅当 WPF 主机应用程序加载和卸载加载项时才会发生异常。我们正在将加载项中的 WindowsFormsHost 控件和基于 Windows 窗体的控件作为卸载加载项之前调用的自定义 Dispose 方法的一部分进行处置。
加载项在卸载之前关闭其调度程序(作为上述处理过程的一部分),MSDN 文档和博客文章中已指出这是必需的,并且还解决了在本例中未发生的问题.
- 别无选择,只能使用基于 Windows 窗体的控件,因为某些报告需要使用它,而且要转换的报告文件太多,而且没有合适的 WPF 版本,也没有时间进行更改。
我无法提供代码示例,因此我正在寻求任何想法或以前对这种情况的经验,以防万一我错过了一些东西。
I am working on a WPF application that is using System.AddIn to solve a memory leak issue we were having with a Windows Forms based control that was wrapped in a WindowsFormsHost control. The add-in is used to load and unload the Windows Forms based control as need to avoid the overhead of the WindowsFormsHost, which will hang around until application close in the current version of WPF and a memory leak in the Windows Forms based control due to bad cleanup logic.
The issue we are facing is that after loading and unloading the add-in in the application the WPF application will throw a Win32 exception of “Invalid Window Handle” on application exit. This would normally not be a terrible concern, however even though it is possible to catch the exception it does not stop Windows from seeing the application as crashed and showing a crash dialog under Windows 7 and this is unacceptable.
In regards to the code the relevant facts are:
The exception only occurs if the add-in is loaded and unloaded by the WPF host application. We are disposing the WindowsFormsHost control and the Windows Forms based control in the add-in as part of a custom Dispose method called prior to unloading of the add-in.
The add-in is shutting down its Dispatchers prior to unloading (as part of the above disposal process) which had been noted in MSDN documentation and blog posts to be required and to also solve this problem which has not happened in this case.
We have no choice in using the Windows Forms based control as it is required for some reporting, and there are too many report files to convert and no suitable WPF version and no time to change it out.
I am not able to supply samples of the code so I am reaching out for any thoughts or previous experiences with such a scenario in case there is something I missed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不久前我也遇到过类似的问题。我看到我在主窗口的
Window_Closing
事件处理程序中调用了Dispatcher.InvokeShutdown
(在测试控件的内容不为空之后),并且似乎记得这是解决方案。I had a similar issue awhile back. I see that I call
Dispatcher.InvokeShutdown
(after testing that the control's content is not null) in my main window'sWindow_Closing
event handler, and seem to recall that it was the solution.