I am working on a WPF application that uses System.AddIn to solve the memory leak problem that we had with the Windows Forms control that was wrapped in a WindowsFormsHost control. The add-in is used to load and unload a Windows Forms-based control, because you must avoid the overhead of WindowsFormsHost, which will hang until the application closes in the current version of WPF and the memory leak in the Windows Forms control due to poor cleaning logic.
The problem we are facing is that after loading and unloading the add-in in the application, the WPF application will throw a Win32 exception “Invalid window handle” when the application exits. This, as a rule, is not a terrible problem, however, although it is possible to catch an exception, it will not prevent Windows from seeing how the application crashed and displaying the crash dialog under Windows 7, and this is unacceptable.
The following facts are in relation to the code:
An exception occurs only if the add-in is loaded and unloaded by the WPF host application. We remove the WindowsFormsHost control and the Windows Forms control in the add-in as part of the Dispose custom method that is called before the add-in is unloaded.
The add-in closes its dispatchers before unloading (as part of the uninstallation process described above), which were noted in the MSDN documentation and blog posts that are necessary, as well as solve this problem, which was not performed in this case.
We have no choice in using the Windows Forms control, as is required for some reports, and there are too many report files to convert and the appropriate version of WPF and the time to change it.
I cannot provide code samples, so I refer to any thoughts or previous experiences with such a scenario if there is something that I missed.
user249374
source share