dotnet/wpf · error · InvalidOperationException
SR.CantPrefetchTextPattern
Error message
SR.CantPrefetchTextPattern
What it means
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.
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.
Example fix
// before var tp = (TextPattern)element.GetCurrentPattern(TextPattern.Pattern); // may throw // after if (!element.TryGetCurrentPattern(TextPattern.Pattern, out var obj)) return null; var tp = (TextPattern)obj;
Defensive patterns
Strategy: try-catch
Validate before calling
if (!element.GetSupportedPatterns().Any(p => p.ProgrammaticName == TextPattern.Pattern.ProgrammaticName)) return null;
Try / catch
try
{
element.GetCurrentPattern(TextPattern.Pattern);
}
catch (InvalidOperationException)
{
element = ReFindElement(searchCriteria); // pattern handle invalid; re-acquire
}
catch (ElementNotAvailableException) { /* element gone */ } Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
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
- Character offset not valid in TextRange.
- E_OUTOFMEMORY
- Element no longer available.
- Element no longer available.
- ElementNotAvailableException
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/b821925f0d2ecb71.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/System/Windows/Automation/TextPattern.cs:275
{
return UiaCoreApi.TextPattern_get_SupportedTextSelection(_hPattern);
}
}
#endregion Public Properties
//------------------------------------------------------
//
// Internal Methods
//
//------------------------------------------------------
#region Internal Methods
internal static object Wrap(AutomationElement el, SafePatternHandle hPattern, bool cached)
{
if (hPattern.IsInvalid)
{
throw new InvalidOperationException(SR.CantPrefetchTextPattern);
}
return new TextPattern(el, hPattern);
}
// compare two text patterns and return true if they are from the same logical element.
internal static bool Compare(TextPattern t1, TextPattern t2)
{
return Misc.Compare(t1._element, t2._element);
}
#endregion Internal Methods
//------------------------------------------------------
//
// Private Fields
//
//------------------------------------------------------View on GitHub (pinned to 81131a70a4)