{"record":{"id":"bf2907375998e8b9","repo":"dotnet/wpf","slug":"sr-cacherequestneedliveforproperties","errorCode":null,"errorMessage":"SR.CacheRequestNeedLiveForProperties","messagePattern":"SR\\.CacheRequestNeedLiveForProperties","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/MS/Internal/Automation/Misc.cs","lineNumber":192,"sourceCode":"                throw new ArgumentException(SR.GetResourceString(reason, null));\n            }\n        }\n\n        // Called by the patterns before accessing .Cache\n        internal static void ValidateCached(bool cached)\n        {\n            if (!cached)\n            {\n                throw new InvalidOperationException(SR.CacheRequestNeedCache);\n            }\n        }\n\n        // Called by the patterns before accessing .Current\n        internal static void ValidateCurrent(SafePatternHandle hPattern)\n        {\n            if (hPattern.IsInvalid)\n            {\n                throw new InvalidOperationException(SR.CacheRequestNeedLiveForProperties);\n            }\n        }\n\n        // Call IsCriticalException w/in a catch-all-exception handler to allow critical exceptions\n        // to be thrown (this is copied from exception handling code in WinForms but feel free to\n        // add new critical exceptions).  Usage:\n        //      try\n        //      {\n        //          Somecode();\n        //      }\n        //      catch (Exception e)\n        //      {\n        //          if (Misc.IsCriticalException(e))\n        //              throw;\n        //          // ignore non-critical errors from external code\n        //      }\n        internal static bool IsCriticalException( Exception e )\n        {","sourceCodeStart":174,"sourceCodeEnd":210,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/MS/Internal/Automation/Misc.cs#L174-L210","documentation":"Misc.ValidateCurrent throws InvalidOperationException(SR.CacheRequestNeedLiveForProperties) when the pattern's underlying SafePatternHandle is invalid, meaning no live (non-cached) pattern instance exists. Accessing pattern.Current requires the pattern to have been obtained with live property access, not only cached data.","triggerScenarios":"Accessing pattern.Current on a pattern retrieved without a live pattern handle — e.g., pattern fetched only under a CacheRequest where Current properties were not requested, or the handle has since been invalidated (element gone).","commonSituations":"Code that retrieves patterns with a cache request (cached-only) and later tries to read .Current for up-to-date values; also occurs when the target element was destroyed and the pattern handle invalidated.","solutions":["Re-acquire the pattern with GetCurrentPattern (live) or include the pattern in the CacheRequest so Current access is valid, then read .Current.","Verify the element is still available (ElementNotAvailable handling) and re-fetch the pattern if the window/pattern handle was invalidated.","If only cached data is needed, use .Cached instead of .Current."],"exampleFix":"// before\nvar pat = (TogglePattern)cachedElement.GetCachedPattern(TogglePattern.Pattern);\nvar state = pat.Current.ToggleState; // may throw if handle invalid\n// after\nvar live = (TogglePattern)element.GetCurrentPattern(TogglePattern.Pattern);\nvar state = live.Current.ToggleState;","handlingStrategy":"try-catch","validationCode":"if (!element.Current.IsOffscreen /* element alive check */)\n{ var pat = element.GetCurrentPattern(Pattern); /* live handle */ }","typeGuard":"static bool HasLivePattern(AutomationElement el, AutomationPattern p) => el != null && el.GetSupportedPatterns().Contains(p);","tryCatchPattern":"try { var v = pat.Current.Value; }\ncatch (InvalidOperationException) { pat = (ValuePattern)element.GetCurrentPattern(ValuePattern.Pattern); v = pat.Current.Value; }\ncatch (ElementNotAvailableException) { /* element gone; re-locate */ }","preventionTips":["Use GetCurrentPattern (live) whenever you need .Current values.","Re-fetch patterns after the target window is recreated or closed/reopened.","Handle ElementNotAvailableException alongside this error in UIA loops."],"tags":["uiautomation","cache-request","invalid-state"],"backgroundTag":"invalid-state-transition","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}