WPF Modal Window renders completely white (blank) in Hybrid WinForms application under load

6 days ago 8
ARTICLE AD BOX

I am maintaining a legacy hybrid application (Main App is WinForms, hosting WPF UserControls via ElementHost).

The Problem:
When opening a WPF Modal Window (Window.ShowDialog()) from the WinForms main thread, the window occasionally opens completely white/blank.

The window border and title bar are visible.

The content area is white.

Crucial observation: The logical tree seems to exist (cursor changes to I-Beam over textboxes, arrow over buttons), but nothing is rendered.

Workaround: If I manually resize the window by dragging the border, the content instantly appears.

The Scenario:
The issue reproduces only after a specific complex scenario involving opening/closing multiple hybrid windows and active background threads (Data Agent, Messenger). It looks like a Message Pump Starvation or a Layout Deadlock between WinForms and WPF Dispatchers.

What I have established so far:

No Exceptions: There are no unhandled exceptions in the Output window. I previously had InvalidOperationException (Cross-thread access) spam, but I fixed it by guarding the heartbeat/background UI updates. The log is clean.

Not a GDI Leak: GDI Objects count is low (~200).

Not a Driver Issue: I have forced System.Windows.Interop.RenderMode.SoftwareOnly, but the white window persists.

What I have tried (and failed):

"Kicking" the window: Added code in Loaded event to InvalidateVisual(), UpdateLayout(), and programmatically change Width by 0.1px via Dispatcher.BeginInvoke (at various priorities: Loaded, Render, ContextIdle).

Result: Programmatic resizing does nothing. Only physical mouse resizing works.

Z-Order/Topmost: Explicitly set Topmost = false to avoid Airspace issues.

Flushing Queues: Before calling .ShowDialog(), I tried calling System.Windows.Forms.Application.DoEvents() and WpfApplication.Current.Dispatcher.Invoke(...) to clear pending messages.

Pre-initialization: Tried EnsureHandle(), Show(), Hide() sequence before ShowDialog() to force the creation of the underlying HWND and DirectX surface.

Removing SizeToContent: Suspected a conflict between SizeToContent and manual size setting in Loaded, but removing it didn't solve the starvation.
and other infinity attempts .

The Code Structure:
The method used to open the WPF window from WinForms:

code C#

public static bool ShowContextedWindow(object viewModel) { var win = new DialogWindow() { Content = viewModel, DataContext = viewModel, WindowStartupLocation = WindowStartupLocation.CenterScreen, Topmost = false }; var helper = new WindowInteropHelper(win); helper.Owner = System.Windows.Forms.Form.ActiveForm.Handle; ElementHost.EnableModelessKeyboardInterop(win); System.Windows.Forms.Application.DoEvents(); return win.ShowDialog() == true; }

My Question:
Why does the WPF rendering pipeline freeze (fail to process WM_PAINT?) when opened modally from WinForms under load, and why does programmatic resizing fail to wake it up, while manual resizing works? Are there specific "Nuclear options" to force the WPF Dispatcher to process the render queue in this specific hybrid deadlock scenario?


Read Entire Article