Problem
The WinUI 3 tray app crashes intermittently when clicking the tray icon to show the menu. The crash occurs in the native WinUI windowing layer and bypasses all .NET exception handlers.
Crash Details
- Faulting module:
Microsoft.UI.Windowing.Core.dll (version 10.0.27108.1025)
- Exception code:
0xe0464645 (CLR exception marker)
- Fault offset:
0x000000000000a280 (consistent across all crashes)
- Platform: Windows 11 ARM64
- Windows App SDK: 1.8.250906003
- WinUIEx: 2.9.0
- .NET: 9.0 (targeting windows10.0.19041.0)
Reproduction Steps
- Start the tray app
- Click tray icon - menu appears (works)
- Click away to dismiss menu
- Wait a few seconds OR open Settings window and close it
- Click tray icon again - CRASH
The crash doesn't happen immediately but becomes more likely after:
- The app has been idle for a period
- Other windows have been opened/closed (like Settings)
- Multiple menu show/hide cycles
What We've Tried
1. Window Reuse Pattern
Instead of creating/destroying TrayMenuWindow each click, tried reusing a single window:
- Hide() instead of Close() on deactivation
- ClearItems() and rebuild menu content
- Result: Black square appeared instead of menu content
2. UI Thread Dispatching
Added checks to ensure tray icon callbacks dispatch to UI thread
3. Crash Logging
Added UnhandledException, DomainUnhandledException, UnobservedTaskException handlers
- Result: No crash log written - crash is in native code
4. Backdrop Removal
Removed DesktopAcrylicBackdrop from TrayMenuWindow
5. Async Data Fetch with Timeout
Added brief wait for gateway data before showing menu
Current Architecture
TrayMenuWindow extends WinUIEx.WindowEx, configured as popup-style (no title bar, always on top, closes on deactivation). ShowAtCursor() uses P/Invoke to position near taskbar.
App creates new TrayMenuWindow on each tray click, closes previous one first.
Hypothesis
The crash appears to be related to WinUI window lifecycle management:
- GC collecting something the native windowing layer still references after idle
- COM apartment threading issue when tray icon callback fires
- WindowEx/WinUIEx issue with rapid window creation/destruction
- Windows App SDK bug with unpackaged apps and system tray interaction
Potential Solutions to Explore
- MenuFlyout approach: Use
e.Flyout = BuildMenuFlyout() in tray callbacks instead of custom window
- Keep-alive window: Create an invisible window to keep WinUI runtime active
- GC.KeepAlive: Pin critical objects to prevent collection
Files of Interest
src/OpenClaw.Tray.WinUI/App.xaml.cs - Main app, tray icon handling
src/OpenClaw.Tray.WinUI/Windows/TrayMenuWindow.xaml.cs - Popup menu window
Windows Event Log Entry
Faulting application name: OpenClaw.Tray.WinUI.exe
Faulting module name: Microsoft.UI.Windowing.Core.dll
Exception code: 0xe0464645
Fault offset: 0x000000000000a280
Problem
The WinUI 3 tray app crashes intermittently when clicking the tray icon to show the menu. The crash occurs in the native WinUI windowing layer and bypasses all .NET exception handlers.
Crash Details
Microsoft.UI.Windowing.Core.dll(version 10.0.27108.1025)0xe0464645(CLR exception marker)0x000000000000a280(consistent across all crashes)Reproduction Steps
The crash doesn't happen immediately but becomes more likely after:
What We've Tried
1. Window Reuse Pattern
Instead of creating/destroying TrayMenuWindow each click, tried reusing a single window:
2. UI Thread Dispatching
Added checks to ensure tray icon callbacks dispatch to UI thread
3. Crash Logging
Added UnhandledException, DomainUnhandledException, UnobservedTaskException handlers
4. Backdrop Removal
Removed DesktopAcrylicBackdrop from TrayMenuWindow
5. Async Data Fetch with Timeout
Added brief wait for gateway data before showing menu
Current Architecture
TrayMenuWindow extends WinUIEx.WindowEx, configured as popup-style (no title bar, always on top, closes on deactivation). ShowAtCursor() uses P/Invoke to position near taskbar.
App creates new TrayMenuWindow on each tray click, closes previous one first.
Hypothesis
The crash appears to be related to WinUI window lifecycle management:
Potential Solutions to Explore
e.Flyout = BuildMenuFlyout()in tray callbacks instead of custom windowFiles of Interest
src/OpenClaw.Tray.WinUI/App.xaml.cs- Main app, tray icon handlingsrc/OpenClaw.Tray.WinUI/Windows/TrayMenuWindow.xaml.cs- Popup menu windowWindows Event Log Entry