MahApps / MahApps/MahApps.Metro

Windows 11 SaveWindowState Exception

Open
#4,557 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug
Dominant language
C#
Stars
9.8k
Forks
2.4k
Avg merge
1h 2m
Merged PRs (30d)
56

Description

Describe the bug

In an Application im using MahApps, and on Windows 11 this Error sometimes occurs.
We had added SaveWindowPosition="False" but it didn't change the behaviour.
We are not trying to access a Window or similar from another Thread. The same code works under Windows 10 without errors.

Steps to reproduce

We can reproduce it by starting the application and leaving it open. Then Log off the user from Windows. Windows shows a message that the application stops the Log off process.

Expected behavior

The Application closes and the user logs off. And the exception does not occur anywhere.

Actual behavior

An exception occurs

Environment

MahApps.Metro version: v2.4.11
Windows build number: Win11 or Windows build number: Windows Server 2025 Datacenter 24H2 [26100.4652]
Visual Studio: 2022 Version 17.8.4
Target Framework: .Net Framework 4.8

Screenshots

The calling thread cannot access this object because a different thread owns it

at System.Windows.Threading.Dispatcher.VerifyAccess()
at MahApps.Metro.Behaviors.WindowsSettingBehavior.SaveWindowState()
at System.Windows.Application.OnSessionEnding(SessionEndingCancelEventArgs e)
at System.Windows.Application.WmQueryEndSession(IntPtr lParam, IntPtr& refInt)
at System.Windows.Application.AppFilterMessage(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 MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
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)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with MahApps.Metro.Behaviors.WindowsSettingBehavior.SaveWindowState() and the session-ending call path shown in the stack trace. Reproduce the issue by leaving the application open and logging off Windows 11, then investigate the dispatcher access during shutdown. Done means the application closes and logoff completes without the exception.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
desktop, operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.