dotnet / dotnet/wpf

WPF NET 9.0 (latest preview)-based app that uses the built-in Fluent theme crashes when Theme is changed

Open
#9,906 17 comments 3 reactions 1 assignee Claimed by @dipeshmsft View on GitHub
Investigate Priority:1 Win 11 Theming
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

### Description

This problem appears on two kinds of OS:
* Windows 10 (AMD64, 24H2)
* Windows 11 (ARM64, 24H2)

Here's the error I've got in the beginning on both machines:
```
Monday, October 7, 2024, 10/7/2024 12:48:28 PM
Exception [System.InvalidOperationException]: The calling thread cannot access this object because a different thread owns it.
System.InvalidOperationException: The calling thread cannot access this object because a different thread owns it.
at System.Windows.Threading.Dispatcher.g__ThrowVerifyAccess|7_0()
at System.Windows.ThemeManager.OnSystemThemeChanged()
at System.Windows.SystemResources.SystemThemeFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
```

Sadly there was no way to figure out why this error happened. My app showed this message and then crashed. App uses Microsoft.VisualBasic library to get a Single Instance behavior, so catching the exception would get us to app.Run method.

After several experiments, the app now crashes w/o even this error after the theme of the apps in System Settings is changed to Light/Dark.

### Reproduction Steps

1. Create a WPF 9.0 app that uses Visual Basic assembly's Application to get a Single Instance Application
2. Use Fluent theme as a ResourceDictionary across your app
3. Switch theme of all apps in System Settings from Light to Dark or vice versa

### Expected behavior

Theme changes where expected

### Actual behavior

App crashes (initially with an error message and now silently)

### Regression?

there were no problems prior to this moment

### Known Workarounds

1. don't use Fluent theme?
2. Added these lines in SingleInstanceApplication's OnStartup override event handler:

```
#pragma warning disable WPF0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
this.ThemeMode = ThemeMode.Light;
#pragma warning restore WPF0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
```

This seems to keep app working.

### Impact

App cannot be given to the end users. It crashes every time user changes an app color in System Settings on Windows 11, and it crashes (in general) every time the theme changes on Windows 10/11.

### Configuration

.NET 9 (preview, latest)
Windows 10: 24H2, amd64
Windows 11: 24H2 Canary, arm64

### Other information

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.