I used the next code to wait for window opened:
Automation.AddAutomationEventHandler(
WindowPattern.WindowOpenedEvent,
AutomationElement.RootElement,
TreeScope.Children,
(sender, e) =>
{
var element = sender as AutomationElement;
if (element.Current.Name != rolesFormTitle)
return;
Automation.RemoveAllEventHandlers();
SelectRoleOpenMainForm();
});
After adding reference to UIAComWrapper v1.1.0.14 (using Nuget) I started getting this exception (source code of UIAComWrapper):
Inner exception is null. The same exception rises for Factory.RemoveAllEventHandlers();
Should I prepare the state of some object somehow?
UPDATE1:
Stack trace:
at UIAutomationClient.CUIAutomation8Class.RemoveAutomationEventHandler(Int32 eventId, IUIAutomationElement element, IUIAutomationEventHandler handler)
at System.Windows.Automation.Automation.RemoveAutomationEventHandler(AutomationEvent eventId, AutomationElement element, AutomationEventHandler eventHandler) in Automation.cs: line 239
Source code of Automation.cs (github):
public static void RemoveAutomationEventHandler(AutomationEvent eventId, AutomationElement element, AutomationEventHandler eventHandler)
{
Utility.ValidateArgumentNonNull(element, "element");
Utility.ValidateArgumentNonNull(eventHandler, "eventHandler");
Utility.ValidateArgument(eventId != AutomationElement.AutomationFocusChangedEvent, "Use FocusChange notification instead");
Utility.ValidateArgument(eventId != AutomationElement.StructureChangedEvent, "Use StructureChange notification instead");
Utility.ValidateArgument(eventId != AutomationElement.AutomationPropertyChangedEvent, "Use PropertyChange notification instead");
try
{
BasicEventListener listener = (BasicEventListener)ClientEventList.Remove(eventId, element, eventHandler);
Factory.RemoveAutomationEventHandler(eventId.Id, element.NativeElement, listener); // line 239
}
catch (System.Runtime.InteropServices.COMException e)
{
Exception newEx; if (Utility.ConvertException(e, out newEx)) { throw newEx; } else { throw; }
}
}
UPDATE2:
Source code:
private void Form1_Load(object send, EventArgs ev)
{
Process.Start("calc");
Automation.AddAutomationEventHandler(
WindowPattern.WindowOpenedEvent,
AutomationElement.RootElement,
TreeScope.Children, (sender, e) =>
{
var element = sender as AutomationElement;
if (!element.Current.Name.StartsWith("Calculator"))
return;
Automation.RemoveAllEventHandlers();
MessageBox.Show("BINGO!");
});
}
I never see the message "BINGO!". It hangs on Automation.RemoveAllEventHandlers();
UPDATE3:
Still hangs:
private void Form1_Load(object send, EventArgs ev)
{
Process.Start("calc");
Automation.AddAutomationEventHandler(
WindowPattern.WindowOpenedEvent,
AutomationElement.RootElement,
TreeScope.Children, (sender, e) =>
{
var element = sender as AutomationElement;
if (!element.Current.Name.StartsWith("Calculator"))
return;
this.Invoke(new Action(() => RemoveTry()));
});
}
private void RemoveTry()
{
Automation.RemoveAllEventHandlers(); // reachable
MessageBox.Show("BINGO!"); // unreachable
}
Threads window:
SOLUTION FOUND:
Please, see comments for the solution found.