dotnet/wpf · error · ArgumentException

SR.ParamsNotApplicableToWindowClosedEvent

Error message

SR.ParamsNotApplicableToWindowClosedEvent

What it means

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.

Solutions

  1. Subscribe to WindowClosedEvent using only the parameters the API allows (default cache request); do not pass a custom CacheRequest
  2. If you need data about the window before it closes, listen to WindowOpened/WindowPattern.WindowClosingEvent or cache properties beforehand
  3. Handle the extra parameters explicitly: build the subscription with eventId != WindowClosed for scoped/cache cases

Example fix

// before
Automation.AddAutomationEventHandler(WindowPattern.WindowClosedEvent, root, TreeScope.Subtree, handler, customCacheRequest);
// after
Automation.AddAutomationEventHandler(WindowPattern.WindowClosedEvent, root, TreeScope.Children, handler);
Defensive patterns

Strategy: validation

Validate before calling

if (eventId == WindowPattern.WindowClosedEvent)
    throw new InvalidOperationException("Use only default parameters with WindowClosedEvent");

Try / catch

try { Automation.AddAutomationEventHandler(eventId, element, scope, handler); }
catch (ArgumentException ex) when (ex.Message.Contains("WindowClosed")) { FixSubscriptionParameters(); }

Prevention

When it happens

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

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

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/System/Windows/Automation/Automation.cs:171

                        paramsValidated = true;
                    }
                    else if ( ( scope & TreeScope.Element ) == TreeScope.Element )
                    {
                        // ...OR Element where the element implements WindowPattern
                        object val = element.GetCurrentPropertyValue(AutomationElement.NativeWindowHandleProperty);
                        if ( val != null && val is int && (int)val != 0 )
                        {
                            if ( HwndProxyElementProvider.IsWindowPatternWindow( NativeMethods.HWND.Cast( new IntPtr( (int)val ) ) ) )
                            {
                                paramsValidated = true;
                            }
                        }
                    }
                }

                if ( !paramsValidated )
                {
                    throw new ArgumentException( SR.ParamsNotApplicableToWindowClosedEvent );
                }
            }

            // Add a client-side Handler for for this event request
            EventListener l = new EventListener(eventId, scope, null, CacheRequest.CurrentUiaCacheRequest);
            ClientEventManager.AddListener(element, eventHandler, l);
        }


        /// <summary>
        /// Called by a client to remove a listener for pattern or custom events.
        /// </summary>
        /// <param name="eventId">a UIAccess or custom event identifier.</param>
        /// <param name="element">Element to remove listener for</param>
        /// <param name="eventHandler">The handler object that was passed to AddEventListener</param>
        public static void RemoveAutomationEventHandler(
            AutomationEvent eventId,
            AutomationElement element,

View on GitHub (pinned to 81131a70a4)