{"record":{"id":"63939d13a0280abe","repo":"dotnet/wpf","slug":"sr-operationcannotbeperformed","errorCode":null,"errorMessage":"SR.OperationCannotBePerformed","messagePattern":"SR\\.OperationCannotBePerformed","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/MS/Internal/Automation/HwndProxyElementProvider.cs","lineNumber":299,"sourceCode":"        //\n        //  Interface IWindowProvider\n        //\n        //------------------------------------------------------\n\n        #region Interface IWindowProvider\n\n        void IWindowProvider.SetVisualState( WindowVisualState state )\n        {\n            if ( !SafeNativeMethods.IsWindow( _hwnd ) )\n                throw new ElementNotAvailableException();\n\n            switch ( state )\n            {\n                case WindowVisualState.Normal:\n                {\n                    // you can't really do anything to a disabled window\n                    if ( IsBitSet(GetWindowStyle(), SafeNativeMethods.WS_DISABLED) )\n                        throw new InvalidOperationException(SR.OperationCannotBePerformed);\n\n                    // If already in the normal state, do not need to do anything.\n                    if (((IWindowProvider)this).VisualState == WindowVisualState.Normal)\n                    {\n                        return;\n                    }\n\n                    ClearMenuMode();\n                        UnsafeNativeMethods.WINDOWPLACEMENT wp = new UnsafeNativeMethods.WINDOWPLACEMENT\n                        {\n                            length = Marshal.SizeOf(typeof(UnsafeNativeMethods.WINDOWPLACEMENT))\n                        };\n\n                        // get the WINDOWPLACEMENT information\n                        if (!Misc.GetWindowPlacement(_hwnd, ref wp))\n                    {\n                        throw new InvalidOperationException(SR.OperationCannotBePerformed);\n                    }","sourceCodeStart":281,"sourceCodeEnd":317,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/MS/Internal/Automation/HwndProxyElementProvider.cs#L281-L317","documentation":"When SetVisualState(WindowVisualState.Normal) is requested, the provider refuses if the window's style has WS_DISABLED set: a disabled window cannot respond to state changes, so InvalidOperationException(SR.OperationCannotBePerformed) is thrown from HwndProxyElementProvider.SetVisualState (HwndProxyElementProvider.cs:299).","triggerScenarios":"Calling IWindowProvider.SetVisualState(WindowVisualState.Normal) on a window whose GWL_STYLE contains WS_DISABLED (window created disabled or disabled via EnableWindow(hwnd,false)).","commonSituations":"Automating modal dialogs or windows whose owner disabled them to block input; apps that call EnableWindow(false) during busy operations; test frameworks trying to restore a disabled splash/progress window.","solutions":["Re-enable the window first (EnableWindow(hwnd, true) from the owning process, or via the app's own UI) before requesting WindowVisualState.Normal.","Skip the restore operation when IsWindowEnabled indicates the window is disabled and wait for the app to re-enable it.","If you control the target app, stop disabling the main window while automation is active, or handle WM_SC commands regardless.","Catch InvalidOperationException from WindowPattern.SetVisualState and fall back to SetWindowPlacement only if you own the process."],"exampleFix":"// before\nwindowPattern.SetVisualState(WindowVisualState.Normal); // throws if window is WS_DISABLED\n\n// after\nif ((GetWindowLong(hwnd, GWL_STYLE) & WS_DISABLED) == 0)\n{\n    windowPattern.SetVisualState(WindowVisualState.Normal);\n}\nelse\n{\n    // window is disabled; enable it in the target app first or wait\n}","handlingStrategy":"validation","validationCode":"bool canRestore = !IsBitSet(GetWindowLong(hwnd, GWL_STYLE), WS_DISABLED); if (!canRestore) throw new SkipException(\"window disabled\");","typeGuard":null,"tryCatchPattern":"try { windowPattern.SetVisualState(WindowVisualState.Normal); } catch (InvalidOperationException) { /* window is WS_DISABLED; wait or skip */ }","preventionTips":["Check WindowPattern properties (CanMinimize/CanMaximize) and window style before state changes","Avoid disabling main windows with EnableWindow(false) while automation is active","Wait for busy/disabled states to clear before issuing window commands","Treat InvalidOperationException from SetVisualState as 'operation not applicable', not a test failure"],"tags":["uiautomation","window-pattern","disabled-window","win32"],"backgroundTag":"unsupported-operation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}