dotnet/wpf · error · InvalidOperationException

SR.CacheRequestNeedLiveForProperties

Error message

SR.CacheRequestNeedLiveForProperties

What it means

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.

Solutions

  1. Re-acquire the pattern with GetCurrentPattern (live) or include the pattern in the CacheRequest so Current access is valid, then read .Current.
  2. Verify the element is still available (ElementNotAvailable handling) and re-fetch the pattern if the window/pattern handle was invalidated.
  3. If only cached data is needed, use .Cached instead of .Current.

Example fix

// before
var pat = (TogglePattern)cachedElement.GetCachedPattern(TogglePattern.Pattern);
var state = pat.Current.ToggleState; // may throw if handle invalid
// after
var live = (TogglePattern)element.GetCurrentPattern(TogglePattern.Pattern);
var state = live.Current.ToggleState;
Defensive patterns

Strategy: try-catch

Validate before calling

if (!element.Current.IsOffscreen /* element alive check */)
{ var pat = element.GetCurrentPattern(Pattern); /* live handle */ }

Type guard

static bool HasLivePattern(AutomationElement el, AutomationPattern p) => el != null && el.GetSupportedPatterns().Contains(p);

Try / catch

try { var v = pat.Current.Value; }
catch (InvalidOperationException) { pat = (ValuePattern)element.GetCurrentPattern(ValuePattern.Pattern); v = pat.Current.Value; }
catch (ElementNotAvailableException) { /* element gone; re-locate */ }

Prevention

When it happens

Trigger: 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).

Common situations: 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.

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


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/bf2907375998e8b9. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/MS/Internal/Automation/Misc.cs:192

                throw new ArgumentException(SR.GetResourceString(reason, null));
            }
        }

        // Called by the patterns before accessing .Cache
        internal static void ValidateCached(bool cached)
        {
            if (!cached)
            {
                throw new InvalidOperationException(SR.CacheRequestNeedCache);
            }
        }

        // Called by the patterns before accessing .Current
        internal static void ValidateCurrent(SafePatternHandle hPattern)
        {
            if (hPattern.IsInvalid)
            {
                throw new InvalidOperationException(SR.CacheRequestNeedLiveForProperties);
            }
        }

        // Call IsCriticalException w/in a catch-all-exception handler to allow critical exceptions
        // to be thrown (this is copied from exception handling code in WinForms but feel free to
        // add new critical exceptions).  Usage:
        //      try
        //      {
        //          Somecode();
        //      }
        //      catch (Exception e)
        //      {
        //          if (Misc.IsCriticalException(e))
        //              throw;
        //          // ignore non-critical errors from external code
        //      }
        internal static bool IsCriticalException( Exception e )
        {

View on GitHub (pinned to 81131a70a4)