WindowsFormsHost performance
The WindowsFormsHost control offered in WPF has many performance penalties that will catch out naïve users. Here are some tips and tricks to improve performance.
Don’t use a transparent background
By default, WindowsFormsHost will attempt to emulate a Transparent
background by rendering the WPF control behind it into a backbuffer and using that backbuffer as its own background.
Obviously, this is bad for memory consumption, but it also costs a surprising amount of time when first displaying the WindowsFormsHost. This can add up when trying to Show a WPF window containing many WindowsFormsHost controls.
Setting the Background of a WindowsFormsHost to a solid colour, especially if the control paints its own background, is an easy way to keep them performant.
Call Dispose
If you are in a memory-constrained environment, it’s important to note that WindowsFormsHost is by default only disposed on application exit. To fix this, you can pay attention to when the host is no longer required and call Dispose
upon it to release resources owned by it.
Note that calling Dispose on the host will also dispose the child control.
As always, the general caveats about .NET events counting as a strong reference apply. Consider using the Weak Event pattern where possible if you think this may be the case.