UI Automation Events for Clients
This topic describes how Microsoft UI Automation events are used by UI Automation clients.
UI Automation allows clients to subscribe to events of interest. This capability improves performance by eliminating the need to continually poll all the UI elements in the system to see if any information, structure, or state has changed.
Efficiency is also improved by the ability to listen for events only within a defined scope. For example, a client can listen for focus change events on all UI Automation elements in the tree, or on just one element and its descendants.
Do not assume that all possible events are raised by a Microsoft UI Automation provider. For example, not all property changes cause events to be raised by the standard proxy providers for Windows Forms and Win32 controls.
For a broader view of UI Automation events, see UI Automation Events Overview.
This topic contains the following sections.
Subscribing to Events
Client applications subscribe to events of a particular kind by registering an event handler, using one of the following methods.
|Method||Event Type||Event Arguments Type||Delegate Type|
All other events, identified by an AutomationEvent
Before calling the method, you must create a delegate method to handle the event. If you prefer, you can handle different kinds of events in a single method, and pass this method in multiple calls to one of the methods in the table. For example, a single AutomationEventHandler can be set up to handle various events differently according to the EventId.
To process window-closed events, cast the argument type that is passed to the event handler as WindowClosedEventArgs. Because the Microsoft UI Automation element for the window is no longer valid, you cannot use the sender parameter to retrieve information; use GetRuntimeId instead.
If your application might receive events from its own UI, do not use your application's UI thread to subscribe to events, or to unsubscribe. Doing so might lead to unpredictable behavior. For more information, see UI Automation Threading Issues.
On shutdown, or when UI Automation events are no longer of interest to the application, UI Automation clients should call one of the following methods.
Unregisters an event handler that was registered by using AddAutomationEventHandler.
Unregisters an event handler that was registered by using AddAutomationFocusChangedEventHandler.
Unregisters an event handler that was registered by using AddAutomationPropertyChangedEventHandler.
Unregisters all registered event handlers.
For example code, see Subscribe to UI Automation Events.