Share via


UI Automation Threading Issues

Note

This documentation is intended for .NET Framework developers who want to use the managed UI Automation classes defined in the System.Windows.Automation namespace. For the latest information about UI Automation, see Windows Automation API: UI Automation.

Because of the way Microsoft UI Automation uses Windows messages, conflicts can occur when a client application attempts to interact with its own UI on the UI thread. These conflicts can lead to very slow performance or even cause the application to stop responding.

If your client application is intended to interact with all elements on the desktop, including its own UI, you should make all UI Automation calls on a separate thread. This includes locating elements (for example, by using TreeWalker or the FindAll method) and using control patterns.

It is safe to make UI Automation calls within a UI Automation event handler, because the event handler is always called on a non-UI thread. However, when subscribing to events that may originate from your client application's UI, you must make the call to AddAutomationEventHandler, or a related method, on a non-UI thread. Remove event handlers on the same thread.