dotnet/wpf · warning · ElementNotAvailableException

ElementNotAvailableException

Error message

ElementNotAvailableException

What it means

ElementProxy.GetPatternProvider is the UI Automation remote provider bridge: it resolves the underlying AutomationPeer and, if the peer is gone (element destroyed or no longer available on the dispatcher), throws ElementNotAvailableException per UIA contract to signal the element is no longer usable.

Solutions

  1. Catch ElementNotAvailableException in UIA client code and treat the element as stale — re-query the tree for the current element.
  2. Ensure the automation client re-resolves elements after UI updates instead of caching proxies across layout changes.
  3. In provider code, keep the AutomationPeer alive as long as the proxy may be queried (the element's lifetime, not just the call).
  4. In tests, wait for UI readiness (Dispatcher idle / virtualization complete) before issuing automation calls.

Example fix

// before
var value = (string)element.GetCurrentPropertyValue(ValuePattern.ValueProperty);
// after
try
{
    var value = (string)element.GetCurrentPropertyValue(ValuePattern.ValueProperty);
}
catch (ElementNotAvailableException)
{
    element = tree.Refresh(element); // element went away; re-resolve
}
Defensive patterns

Strategy: try-catch

Validate before calling

// client-side: re-resolve element before use
if (element == null || !element.Current.IsAvailable) // may itself throw; wrap in try
    element = FindElementAgain();

Type guard

bool IsAlive(System.Windows.Automation.AutomationElement el)
{
    try { var _ = el.Current.Name; return true; }
    catch (ElementNotAvailableException) { return false; }
}

Try / catch

try
{
    var pattern = (ValuePattern)element.GetCurrentPattern(ValuePattern.Pattern);
}
catch (ElementNotAvailableException)
{
    element = FindElementAgain(); // stale proxy: re-query the tree
}

Prevention

When it happens

Trigger: A UIA client (or WPF's automation infrastructure) queries a pattern on an ElementProxy whose AutomationPeer has been disconnected — typically the corresponding UIElement was removed from the visual tree, its window closed, or the peer was collected before the cross-thread DispatcherOperation ran.

Common situations: UIA clients (inspect.exe, screen readers, automated tests) racing with UI teardown; tests asserting properties on controls after the window closed; ItemsControl virtualization discarding containers mid-automation call.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Automation/ElementProxy.cs:80

        // to UIAutomation.
        //
        // Interface IRawElementProviderFragmentRoot 'derives' from
        // IRawElementProviderSimple and IRawElementProviderFragment,
        // methods from those appear first below.
        //
        // Most of the interface methods on this interface need
        // to get onto the Visual's context before they can access it,
        // so use Dispatcher.Invoke to call a private method (in the
        // private methods section of this class) that does the real work.

        // IRawElementProviderSimple methods...

        public object GetPatternProvider ( int pattern )
        {
            AutomationPeer peer = Peer;
            if (peer == null)
            {
                throw new ElementNotAvailableException();
            }
            return ElementUtil.Invoke(peer, new DispatcherOperationCallback( InContextGetPatternProvider ), pattern);
        }

        public object GetPropertyValue(int property)
        {
            AutomationPeer peer = Peer;
            if (peer == null)
            {
                throw new ElementNotAvailableException();
            }
            return ElementUtil.Invoke(peer, new DispatcherOperationCallback(InContextGetPropertyValue), property);
        }

        public ProviderOptions ProviderOptions
        {
            get 
            {

View on GitHub (pinned to 81131a70a4)