"Invalid Window Handle" on exiting of WPF application that uses System.AddIn
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 cont开发者_开发问答rol 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.
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's Window_Closing
event handler, and seem to recall that it was the solution.
精彩评论