{"record":{"id":"de0e962eae22bb39","repo":"dotnet/wpf","slug":"sr-cacherequestneedelementreference","errorCode":null,"errorMessage":"SR.CacheRequestNeedElementReference","messagePattern":"SR\\.CacheRequestNeedElementReference","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/System/Windows/Automation/AutomationElement.cs","lineNumber":1104,"sourceCode":"        }\n\n\n        #endregion Public Properties\n\n\n        //------------------------------------------------------\n        //\n        //  Internal Methods\n        //\n        //------------------------------------------------------\n \n        #region Internal Methods\n\n        internal void CheckElement()\n        {\n            if (_hnode == null || _hnode.IsInvalid)\n            {\n                throw new InvalidOperationException(SR.CacheRequestNeedElementReference);\n            }\n        }\n\n        // Called by the treewalker classes to navigate - we call through to the\n        // provider wrapper, which gets the navigator code to do its stuff\n        internal AutomationElement Navigate(NavigateDirection direction, Condition condition, CacheRequest request)\n        {\n            CheckElement();\n\n            UiaCoreApi.UiaCacheRequest cacheRequest;\n            if (request == null)\n                cacheRequest = CacheRequest.DefaultUiaCacheRequest;\n            else\n                cacheRequest = request.GetUiaCacheRequest();\n\n            UiaCoreApi.UiaCacheResponse response = UiaCoreApi.UiaNavigate(_hnode, direction, condition, cacheRequest);\n            return CacheHelper.BuildAutomationElementsFromResponse(cacheRequest, response);\n        }","sourceCodeStart":1086,"sourceCodeEnd":1122,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/System/Windows/Automation/AutomationElement.cs#L1086-L1122","documentation":"CheckElement validates that the AutomationElement holds a valid native UIA node handle (_hnode). If the handle is null or invalid the element cannot back any cache request or navigation, so it throws InvalidOperationException(SR.CacheRequestNeedElementReference).","triggerScenarios":"Using an AutomationElement whose runtime node handle was never initialized or has become invalid — e.g. an element constructed around a disposed/invalid SafeNodeHandle, or an element for a node that was removed from the UIA tree.","commonSituations":"Holding element references across window teardown; caching elements whose target window/application exited; marshaling elements across threads or reusing stale cached elements.","solutions":["Re-acquire the element (FindFirst/FromHandle) instead of reusing the stale instance","Check that the target window/application is still alive before using the element","Catch InvalidOperationException and rebuild the element reference"],"exampleFix":"// before\nstaleElement.CheckElement(); // throws if handle invalid\n// after\nAutomationElement element = AutomationElement.FromHandle(hwnd)\n    ?? await ReacquireElementAsync();","handlingStrategy":"validation","validationCode":"static bool HasValidHandle(AutomationElement e) =>\n    e is AutomationElement el && el.IsHandleValid(); // wrap CheckElement in a non-throwing check","typeGuard":"null","tryCatchPattern":"try { el.CheckElement(); }\ncatch (InvalidOperationException) { el = ReacquireElement(originalQuery); }","preventionTips":["Re-acquire elements after window/app teardown instead of reusing references","Avoid storing AutomationElement instances longer than the target UI's lifetime","Wrap long-lived element references in a holder that re-resolves on demand"],"tags":["uiautomation","wpf","invalid-handle","element-lifetime"],"backgroundTag":"resource-not-found","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"}