Windows UIAutomation sometimes hangs other apps

1

I'm using UIAutomation via COM interface in my WPF client app. Some users sometimes are taking hangs of their specific apps (like nVidia Control Panel). My app recieves that exception:

System.Runtime.InteropServices.COMException (0x80131505): Operation timed out. (Exception from HRESULT: 0x80131505) in UIAutomationClient.IUIAutomationElement.FindAll (TreeScope scope, IUIAutomationCondition condition).

I cannot reproduce this on my PC.

  • Do you have experience with that kind of problems?
  • Do you have any workarounds for that cases?
  • Can I somehow decrease COM timeout? maybe this will fix the issue
c#
com
ui-automation
microsoft-ui-automation
asked on Stack Overflow Dec 14, 2017 by Mikolaytis

1 Answer

3

I've found some info about the issue.

  • UIAutomation is unstable only on Windows 10 with Creator's Update (source)
  • This is bug and it's submitted here
  • NickAb found a solution: Use TreeWalker (source)

Both FindAll and FindFirst are prone to this exception, which is raised when trying to find elements in WebView that has loaded its data recently, exception is thrown when methods try to build cache, so it might be due to trying to iterate visual tree while it is being rebuild by another thread.

Replacing FindAll with

private IEnumerable<WiniumElement> GetChildrens()
{
    var elementNode = TreeWalker.ControlViewWalker.GetFirstChild(this.AutomationElement);

    while (elementNode != null)
    {
        yield return new WiniumElement(elementNode);
        elementNode = TreeWalker.ControlViewWalker.GetNextSibling(elementNode);
    }
}

Solves the problem. Needs further investigation. Most likely we will have to replace both FindFirst and FindAll calls with custom TreeWalker iteration method.

This will solve the problem with not being able to get page source for views with WebViews and not being able to find element in WebView due to same error.

answered on Stack Overflow Dec 14, 2017 by Mikolaytis

User contributions licensed under CC BY-SA 3.0