In my WPF application, I host Win32 content using HwndHost. However, creating a HwndHost does not create the native window. Rather, this is done in the overridden BuildWindowCore()
method which is called sometime later by WPF.
My hosted content needs the window handle of the native window for its own initialization. Unfortunately, there is no way I can force the creation of the window (i.e. having WPF call the BuildWindowCore), so I have a second thread which polls the HwndHost until it has been initialized.
In .NET 4.0 / WPF 4.0, a new method WindowInteropHelper.EnsureHandle()
was added. I had hoped this would resolve the situation, but it only works for a Window, not a HwndHost (which doesn't derive from Window). Do you have a suggestion what I could do instead?
EDIT:
I forgot to add some more constraints for a possible solution:
- The HwndHost is placed in a control which, depending on user settings, can be a child of the application's main window or can be placed in a new Window (via a third-party docking manager). This means that during creation of the window I do not know for sure what the parent Window (and thus its hWnd) will be.
- While the native code needs the hWnd during its initialization, the window is only displayed when the user requests it to be shown (i.e. it is invisible at first). Needing to show the window, only to hide it immediately again, should be avoided, if possible.