{"record":{"id":"25d161d6afeca0d9","repo":"dotnet/wpf","slug":"sr-paramsnotapplicabletowindowclosedevent","errorCode":null,"errorMessage":"SR.ParamsNotApplicableToWindowClosedEvent","messagePattern":"SR\\.ParamsNotApplicableToWindowClosedEvent","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/System/Windows/Automation/Automation.cs","lineNumber":171,"sourceCode":"                        paramsValidated = true;\n                    }\n                    else if ( ( scope & TreeScope.Element ) == TreeScope.Element )\n                    {\n                        // ...OR Element where the element implements WindowPattern\n                        object val = element.GetCurrentPropertyValue(AutomationElement.NativeWindowHandleProperty);\n                        if ( val != null && val is int && (int)val != 0 )\n                        {\n                            if ( HwndProxyElementProvider.IsWindowPatternWindow( NativeMethods.HWND.Cast( new IntPtr( (int)val ) ) ) )\n                            {\n                                paramsValidated = true;\n                            }\n                        }\n                    }\n                }\n\n                if ( !paramsValidated )\n                {\n                    throw new ArgumentException( SR.ParamsNotApplicableToWindowClosedEvent );\n                }\n            }\n\n            // Add a client-side Handler for for this event request\n            EventListener l = new EventListener(eventId, scope, null, CacheRequest.CurrentUiaCacheRequest);\n            ClientEventManager.AddListener(element, eventHandler, l);\n        }\n\n\n        /// <summary>\n        /// Called by a client to remove a listener for pattern or custom events.\n        /// </summary>\n        /// <param name=\"eventId\">a UIAccess or custom event identifier.</param>\n        /// <param name=\"element\">Element to remove listener for</param>\n        /// <param name=\"eventHandler\">The handler object that was passed to AddEventListener</param>\n        public static void RemoveAutomationEventHandler(\n            AutomationEvent eventId,\n            AutomationElement element,","sourceCodeStart":153,"sourceCodeEnd":189,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/System/Windows/Automation/Automation.cs#L153-L189","documentation":"Automation.AddAutomationEventHandler rejects event/property combinations that do not apply when the eventId is WindowPattern.WindowClosedEvent. Window-closed events are processed specially (the element is gone), so cache/event scope parameters beyond allowed ones are invalid. The library throws ArgumentException with SR.ParamsNotApplicableToWindowClosedEvent.","triggerScenarios":"Calling Automation.AddAutomationEventHandler with eventId == WindowPattern.WindowClosedEvent while passing a non-default cacheRequest or scope/parameters that fail the method's validation (e.g. a cache request other than CacheRequest.CurrentUiaCacheRequest defaults).","commonSituations":"Subscribing to WindowClosedEvent with a custom CacheRequest to try to capture element data at close time; copying generic event-subscription code that sets scope/cache options and reusing it for WindowClosed.","solutions":["Subscribe to WindowClosedEvent using only the parameters the API allows (default cache request); do not pass a custom CacheRequest","If you need data about the window before it closes, listen to WindowOpened/WindowPattern.WindowClosingEvent or cache properties beforehand","Handle the extra parameters explicitly: build the subscription with eventId != WindowClosed for scoped/cache cases"],"exampleFix":"// before\nAutomation.AddAutomationEventHandler(WindowPattern.WindowClosedEvent, root, TreeScope.Subtree, handler, customCacheRequest);\n// after\nAutomation.AddAutomationEventHandler(WindowPattern.WindowClosedEvent, root, TreeScope.Children, handler);","handlingStrategy":"validation","validationCode":"if (eventId == WindowPattern.WindowClosedEvent)\n    throw new InvalidOperationException(\"Use only default parameters with WindowClosedEvent\");","typeGuard":null,"tryCatchPattern":"try { Automation.AddAutomationEventHandler(eventId, element, scope, handler); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"WindowClosed\")) { FixSubscriptionParameters(); }","preventionTips":["Special-case WindowClosedEvent subscriptions: no custom cache request","Keep a helper method for WindowClosed with the allowed parameters only"],"tags":["uiautomation","wpf","invalid-argument"],"backgroundTag":"invalid-argument-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}