dotnet/wpf · warning · InvalidOperationException

SR.AutomationDispatcherShutdown

Error message

SR.AutomationDispatcherShutdown

What it means

ElementUtil.Invoke posts the automation call to the peer's dispatcher and waits for completion. If the operation neither completed nor timed out but the dispatcher has started shutting down, the result will never be meaningful, so the library throws InvalidOperationException with SR.AutomationDispatcherShutdown explaining the UI thread dispatcher is shutting down.

Solutions

  1. Ensure UIA client automation completes before closing the window/app — stop automation clients first, then shut down.
  2. Check dispatcher.HasShutdownStarted on the app side before allowing peers to be handed out during exit.
  3. In client code catch InvalidOperationException around automation calls during teardown and treat as benign.
  4. Delay Dispatcher shutdown (e.g. push a shutdown-safe flag) until outstanding automation operations finish.

Example fix

// before
window.Close();
// after
if (!Dispatcher.CurrentDispatcher.HasShutdownStarted)
{
    window.Close(); // stop UIA clients / wait for automation calls before shutting down
}
Defensive patterns

Strategy: try-catch

Validate before calling

if (dispatcher != null && dispatcher.HasShutdownStarted)
    return; // skip automation calls during shutdown

Type guard

bool IsDispatcherUsable(Dispatcher d) => d != null && !d.HasShutdownStarted;

Try / catch

try
{
    peer.RaiseAutomationEvent(AutomationEvents.AutomationFocusChanged);
}
catch (InvalidOperationException ex) when (ex.Message.Contains("shut"))
{
    // app shutting down; swallow or log
}

Prevention

When it happens

Trigger: A UIA client calls a peer API on an element whose dispatcher is in the middle of shutdown (Dispatcher.HasShutdownStarted == true) and the queued dispatcher operation did not produce a result — e.g. calling automation APIs on a control while the application/window is closing.

Common situations: UI Automation running during application exit or window close; automated UI tests that close windows while a screen reader/test client still holds element references; apps calling Dispatcher.BeginInvokeShutdown while UIA clients are active.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Automation/ElementUtil.cs:227

                        completed = true;
                    }
},
                null);
                
            if(completed)
            {
                if(remoteException != null)
                {
                    throw remoteException;
                }
            }
            else
            {
                bool dispatcherInShutdown = dispatcher.HasShutdownStarted;

                if(dispatcherInShutdown)
                {
                    throw new InvalidOperationException(SR.AutomationDispatcherShutdown);
                }
                else
                {
                    throw new TimeoutException(SR.AutomationTimeout);
                }
            }
            
            return retVal;
}

        #endregion Internal Methods

        //------------------------------------------------------
        //
        //  Private Methods
        //
        //------------------------------------------------------

View on GitHub (pinned to 81131a70a4)