Keep getting System.Windows.Automation.ElementNotAvailableException

  accessibility, c++, ui-automation, windows

I’m trying to make logging of cursor hovered menu items in notepad using Automation framework. But when i i hover the cursor on item i get an exception:

enter image description here

AppDomain.CurrentDomain.FirstChanceException += (sender, eventArgs) =>
{
        Debug.WriteLine(eventArgs.Exception.ToString());
};

Automation.AddAutomationEventHandler(WindowPattern.WindowOpenedEvent,
        AutomationElement.FromHandle(Process.GetProcessesByName("notepad").First().MainWindowHandle), 
        TreeScope.Element, OnNotepadControlAction);
}  

I also tried to listen for other events (like AutomationElement events, ItemSelectedPattern, InvokePattern, etc.), result is the same.
The only event that i can get without any exceptions is focus change event.

Thanks in advance for any help!

Source: Windows Questions

LEAVE A COMMENT