dotnet/wpf · error · InvalidOperationException

SR.Format(SR.CompatibilityPreferencesSealed…

Error message

SR.Format(SR.CompatibilityPreferencesSealed, "UseSetWindowPosForTopmostWindows", "FrameworkCompatibilityPreferences")

What it means

Setting FrameworkCompatibilityPreferences.UseSetWindowPosForTopmostWindows after the preferences are sealed throws InvalidOperationException. WPF seals the compatibility switches once the framework begins consuming them so that behavior stays consistent for the lifetime of the process. The value must be set before any window/topmost handling occurs.

Solutions

  1. Set the property at the start of Main() before creating any Window or Application instance
  2. Check FrameworkCompatibilityPreferences.IsSealed before assigning and skip/warn if already sealed
  3. Use a process-level configuration (app.config/environment) applied before WPF initializes

Example fix

// before
private void OnTopmostChanged(bool useSetWindowPos)
{
    FrameworkCompatibilityPreferences.UseSetWindowPosForTopmostWindows = useSetWindowPos; // throws at runtime
}

// after
static void Main()
{
    FrameworkCompatibilityPreferences.UseSetWindowPosForTopmostWindows = true;
    RunApp();
}
Defensive patterns

Strategy: validation

Validate before calling

if (!FrameworkCompatibilityPreferences.IsSealed)
    FrameworkCompatibilityPreferences.UseSetWindowPosForTopmostWindows = true;

Type guard

bool PrefsWritable => !FrameworkCompatibilityPreferences.IsSealed;

Try / catch

try { FrameworkCompatibilityPreferences.UseSetWindowPosForTopmostWindows = v; }
catch (InvalidOperationException) { /* already sealed; log and continue with default */ }

Prevention

When it happens

Trigger: Assigning the static UseSetWindowPosForTopmostWindows property after FrameworkCompatibilityPreferences.IsSealed becomes true, i.e., after WPF initialization (first Window shown, Dispatcher started, or framework code touching the preference).

Common situations: Toggling topmost-window behavior in response to a user setting at runtime, or setting it in App startup event / window constructor instead of before Application.Run.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/8aaebfd9379a027b. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkCompatibilityPreferences.cs:194

        // (c) the child window on a different thread/process tries to show an owned topmost window
        //     (like a popup or tooltip) using ShowWindow().
        // To avoid this window manager bug, this option causes SetWindowPos() to be used instead of
        // ShowWindow() for topmost windows, avoiding condition (c).  Ideally the window manager bug
        // will be fixed, but the risk of making a change there is considered too great at this time.
        #region UseSetWindowPosForTopmostWindows

        private static bool _useSetWindowPosForTopmostWindows = false; // use old behavior by default

        internal static bool UseSetWindowPosForTopmostWindows
        {
            get { return _useSetWindowPosForTopmostWindows; }
            set
            {
                lock (_lockObject)
                {
                    if (_isSealed)
                    {
                        throw new InvalidOperationException(SR.Format(SR.CompatibilityPreferencesSealed, "UseSetWindowPosForTopmostWindows", "FrameworkCompatibilityPreferences"));
                    }

                    _useSetWindowPosForTopmostWindows = value;
                }
            }
        }

        internal static bool GetUseSetWindowPosForTopmostWindows()
        {
            Seal();

            return UseSetWindowPosForTopmostWindows;
        }

        private static void SetUseSetWindowPosForTopmostWindowsFromAppSettings(NameValueCollection appSettings)
        {
            // user can use config file to enable this behavior change
            string s = appSettings["UseSetWindowPosForTopmostWindows"];

View on GitHub (pinned to 81131a70a4)