Hello,
I'm working with the MS UIA framework and its built-in Automation event handling. I'm seeing unexpected behavior on the WindowClosedEvent.
[TestMethod] public void TestMethod1() { Process.Start(@"TestApp.exe"); Thread.Sleep(2000); //sleep to wait for proc to boot with ui
var windowElement = AutomationElement.RootElement.FindFirst(TreeScope.Descendants, new AndCondition(new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window), new PropertyCondition(AutomationElement.NameProperty, "TestApp"), new PropertyCondition(AutomationElement.AutomationIdProperty, "TestAppAutomationId"))); Automation.AddAutomationEventHandler(WindowPattern.WindowClosedEvent, windowElement, TreeScope.Element, OnClose); var windowPattern = (WindowPattern)windowElement.GetCurrentPattern(WindowPattern.Pattern); windowPattern.Close(); Thread.Sleep(2000); //sleep to wait for uia's event threads to fire and finish. } public void OnClose(object sender, AutomationEventArgs e) { Console.WriteLine("test"); //this is run twice during the test }
Perhaps I'm doing something wrong here, but my understanding was that the Console call should be executed once. When I run the above code the Console line is executed twice.
When my TestApp is close there are no special events that it runs. The test app is also only a single WPF window.
I'm running against .NET 4.5.2 on Windows 10.
If I'm doing something wrong here that would cause the handler to fire more than once please let me know.
Thanks.