Microsoft UI Automation: Invoke Pattern Exception

2.8k Views Asked by At

I am trying to click a button using invoke pattern.

InvokePattern ipAddMPButton = (InvokePattern)aeAddMPButton.GetCurrentPattern(InvokePattern.Pattern);
        try
        {
            ipAddMPButton .Invoke();
        }
        catch (System.Runtime.InteropServices.COMException e)
        {
            // TODO
        }

It throws COMException and I can't figure out why?

When I run this code, the button is actually pressed and the functionality works well. But then it just stops for some time, and throws this exception.

2

There are 2 best solutions below

4
Ronak Agrawal On

Try checking aeAddMPButton.GetAllSupportedPatterns() and then try invoking then..

0
o_weisman On

Actually I have found that this is already discussed here: https://social.msdn.microsoft.com/Forums/en-US/673bba3d-27b3-4374-b049-0d2e8ec5e462/hang-with-invokepattern-invoke-in-virtual-pc?forum=windowsaccessibilityandautomation . To summarize, Winforms opening of modal dialog does not play nicely with UIAutomation as it does not return from the Invoke command and only creates the modal dialog which of course prevents it from completing the operation. The proposed solution is to spawn a background thread and activate Invoke (or any other operation) from that thread. I also suggest enforcing some kind of timeout for the completion of the operation as it can be lengthy in some circumstances.