I'm trying to develop library that allows testing application's user interface using microsoft unmanaged UIAutomation API. I'm having hard time with resolving an implementation problem regarding accessing elements from another threads such as event handlers. I'm using interop dll to access UIAutomation, so API initialization looks like this:
static public IUIAutomation automation = new CUIAutomation();
For example: I'm subscribing to window opened event :
automation.AddAutomationEventHandler(UIA_EventIds.UIA_Window_WindowOpenedEventId, automation.GetRootElement(),
TreeScope.TreeScope_Subtree, null, this);
then as I am invoking button in some random WinForms application that opens dialog window I get inside the
HandleAutomationEvent(IUIAutomationElement sender, int eventId)
where I would like to access "YES" button in dialog from using
sender.FindFirst(tree,MyClass.automation.CreatePropertyCondition(UIA_PropertyIds.UIA_NamePropertyId, "YES"));
The search takes ages since it can't access COM object from Single-Threaded Apartment. I cant get the approach from microsoft documentation to work:
https://docs.microsoft.com/en-us/windows/desktop/winauto/uiauto-threading
I've tried subscribing to events from another threads, as well as calling element access functions such as `FindAll` or `FindFirst` from another threads with `[MTAThread]` attribute but I can't get anything to work - the effect is still the same. I wonder if
there an option where the elements would be contained in MTA, even when not thread-safe. I am a total begginer when it comes to C#,WinForms and COM Objects so I would appreciate every bit of information that could help.