dotnet / dotnet/wpf

Circular Reference Exception in Window Style Property When Connecting via Remote Desktop

Open
#11,530 4 comments 1 reaction 0 assignees View on GitHub
Investigate
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

### Description

When connecting to a host running a WPF application via Remote Desktop Protocol (RDP), the application may throw a System.InvalidOperationException indicating a circular reference was detected while calculating the Style property of a Window element.

### Reproduction Steps

1. Launch a WPF application with Fluent theme enabled
2. Connect to the host running the application via Remote Desktop Connection (RDP)
3. Upon establishing the RDP connection, a system theme change event is triggered
4. The application may throws an exception

### Expected behavior

When connecting via Remote Desktop, the application should smoothly handle system theme changes and update Fluent theme resources without throwing exceptions.

### Actual behavior

System.InvalidOperationException: 在计算元素"System.Windows.Window"上的 Style 属性时发现循环引用。
at System.Windows.FrameworkElement.UpdateStyleProperty()
at System.Windows.TreeWalkHelper.InvalidateStyleAndReferences(DependencyObject d, ResourcesChangeInfo info, Boolean containsTypeOfKey)
at System.Windows.TreeWalkHelper.OnResourcesChanged(DependencyObject d, ResourcesChangeInfo info, Boolean raiseResourceChangedEvent)
at System.Windows.TreeWalkHelper.OnResourcesChangedCallback(DependencyObject d, ResourcesChangeInfo info, Boolean visitedViaVisualTree)
at System.Windows.DescendentsWalker`1.StartWalk(DependencyObject startNode, Boolean skipStartNode)
at System.Windows.TreeWalkHelper.InvalidateOnResourcesChange(FrameworkElement fe, FrameworkContentElement fce, ResourcesChangeInfo info)
at System.Windows.Application.InvalidateResourceReferenceOnWindowCollection(WindowCollection wc, ResourcesChangeInfo info)
at System.Windows.Application.InvalidateResourceReferences(ResourcesChangeInfo info)
at System.Windows.ResourceDictionary.NotifyOwners(ResourcesChangeInfo info)
at System.Windows.ResourceDictionary.OnMergedDictionariesChanged(Object sender, NotifyCollectionChangedEventArgs e)
at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
at System.Windows.ThemeManager.AddOrUpdateThemeResources(ResourceDictionary rd, ResourceDictionary newDictionary)
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, Int32 wParam, IntPtr lParam)

### Regression?

_No response_

### Known Workarounds

_No response_

### Impact

The process was terminated due to an unhandled exception.

### Configuration

OS: Windows 10 IoT 企业版 LTSC
.NET Version: .NET 9.0+
WPF Version: PresentationFramework.Fluent
Trigger Scenario: Remote Desktop Connection (RDP)

### Other information

- The IgnoreWindowResourcesChange flag is only set within AddOrUpdateThemeResources
- However, resource change events propagate immediately, triggering window style updates
- The style update attempts to reference the resource dictionary that is currently being replaced, creating a circular dependency

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.