{"record":{"id":"c26aa71dd8c90a0f","repo":"dotnet/wpf","slug":"sr-automationdispatchershutdown","errorCode":null,"errorMessage":"SR.AutomationDispatcherShutdown","messagePattern":"SR\\.AutomationDispatcherShutdown","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"warning","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Automation/ElementUtil.cs","lineNumber":227,"sourceCode":"                        completed = true;\n                    }\n},\n                null);\n                \n            if(completed)\n            {\n                if(remoteException != null)\n                {\n                    throw remoteException;\n                }\n            }\n            else\n            {\n                bool dispatcherInShutdown = dispatcher.HasShutdownStarted;\n\n                if(dispatcherInShutdown)\n                {\n                    throw new InvalidOperationException(SR.AutomationDispatcherShutdown);\n                }\n                else\n                {\n                    throw new TimeoutException(SR.AutomationTimeout);\n                }\n            }\n            \n            return retVal;\n}\n\n        #endregion Internal Methods\n\n        //------------------------------------------------------\n        //\n        //  Private Methods\n        //\n        //------------------------------------------------------\n","sourceCodeStart":209,"sourceCodeEnd":245,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Automation/ElementUtil.cs#L209-L245","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure UIA client automation completes before closing the window/app — stop automation clients first, then shut down.","Check dispatcher.HasShutdownStarted on the app side before allowing peers to be handed out during exit.","In client code catch InvalidOperationException around automation calls during teardown and treat as benign.","Delay Dispatcher shutdown (e.g. push a shutdown-safe flag) until outstanding automation operations finish."],"exampleFix":"// before\nwindow.Close();\n// after\nif (!Dispatcher.CurrentDispatcher.HasShutdownStarted)\n{\n    window.Close(); // stop UIA clients / wait for automation calls before shutting down\n}","handlingStrategy":"try-catch","validationCode":"if (dispatcher != null && dispatcher.HasShutdownStarted)\n    return; // skip automation calls during shutdown","typeGuard":"bool IsDispatcherUsable(Dispatcher d) => d != null && !d.HasShutdownStarted;","tryCatchPattern":"try\n{\n    peer.RaiseAutomationEvent(AutomationEvents.AutomationFocusChanged);\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"shut\"))\n{\n    // app shutting down; swallow or log\n}","preventionTips":["Stop UIA clients before closing windows or exiting the app","Check Dispatcher.HasShutdownStarted before teardown-time automation","Sequence shutdown: cancel automation sessions, then close, then shutdown","In tests, dispose automation clients before application shutdown"],"tags":["wpf","ui-automation","dispatcher","shutdown"],"backgroundTag":"dispatcher-shutdown","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"}