Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Microsoft UI Automation: Invoke Pattern Exception

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.

like image 785
HasnainMamdani Avatar asked Oct 21 '22 02:10

HasnainMamdani


2 Answers

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.

like image 63
o_weisman Avatar answered Oct 24 '22 00:10

o_weisman


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

like image 32
Ronak Agrawal Avatar answered Oct 23 '22 23:10

Ronak Agrawal