{"record":{"id":"b821925f0d2ecb71","repo":"dotnet/wpf","slug":"sr-cantprefetchtextpattern","errorCode":null,"errorMessage":"SR.CantPrefetchTextPattern","messagePattern":"SR\\.CantPrefetchTextPattern","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/System/Windows/Automation/TextPattern.cs","lineNumber":275,"sourceCode":"            {\n                return UiaCoreApi.TextPattern_get_SupportedTextSelection(_hPattern);\n            }\n        }\n\n        #endregion Public Properties\n\n        //------------------------------------------------------\n        //\n        //  Internal Methods\n        //\n        //------------------------------------------------------\n \n        #region Internal Methods\n        internal static object Wrap(AutomationElement el, SafePatternHandle hPattern, bool cached)\n        {\n            if (hPattern.IsInvalid)\n            {\n                throw new InvalidOperationException(SR.CantPrefetchTextPattern);\n            }\n\n            return new TextPattern(el, hPattern);\n        }\n\n        // compare two text patterns and return true if they are from the same logical element.\n        internal static bool Compare(TextPattern t1, TextPattern t2)\n        {\n            return Misc.Compare(t1._element, t2._element);\n        }\n\n        #endregion Internal Methods\n        \n        //------------------------------------------------------\n        //\n        //  Private Fields\n        //\n        //------------------------------------------------------","sourceCodeStart":257,"sourceCodeEnd":293,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/System/Windows/Automation/TextPattern.cs#L257-L293","documentation":"TextPattern.Wrap (internal factory) throws InvalidOperationException when the SafePatternHandle handed to it is invalid — i.e. the text pattern handle could not be prefetched/resolved from the UIA core for the element. This indicates the element no longer exposes the Text pattern or the underlying handle acquisition failed.","triggerScenarios":"Retrieving TextPattern via AutomationElement.GetCurrentPattern(TextPattern.Pattern) (or GetSupportedPattern) from an element whose provider no longer supports Text, or after the element was removed/disconnected so the native pattern handle is invalid.","commonSituations":"Target UI vanished (document closed, window destroyed) between finding the element and fetching the pattern; provider regression where TextPattern.IsAvailable was true but the handle could not be created; race conditions in automation scripts.","solutions":["Check TextPattern.IsAvailable / GetSupportedPatterns immediately before requesting the pattern and again after failures.","Catch InvalidOperationException and ElementNotAvailableException around pattern retrieval and re-find the element via a fresh AutomationElement.","Use AutomationEventHandler for structure changes to stop operating on dead elements."],"exampleFix":"// before\nvar tp = (TextPattern)element.GetCurrentPattern(TextPattern.Pattern); // may throw\n// after\nif (!element.TryGetCurrentPattern(TextPattern.Pattern, out var obj)) return null;\nvar tp = (TextPattern)obj;","handlingStrategy":"try-catch","validationCode":"if (!element.GetSupportedPatterns().Any(p => p.ProgrammaticName == TextPattern.Pattern.ProgrammaticName)) return null;","typeGuard":null,"tryCatchPattern":"try\n{\n    element.GetCurrentPattern(TextPattern.Pattern);\n}\ncatch (InvalidOperationException)\n{\n    element = ReFindElement(searchCriteria); // pattern handle invalid; re-acquire\n}\ncatch (ElementNotAvailableException) { /* element gone */ }","preventionTips":["Check TextPattern.IsAvailable right before fetching the pattern","Subscribe to AutomationEvents.StructureChanged to detect dead elements","Keep pattern retrieval adjacent to element discovery to minimize race windows","Retry with a fresh AutomationElement.FindFirst when the handle is invalid"],"tags":["uiautomation","textpattern","invalid-operation","stale-element"],"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"}