简体   繁体   中英

Automation Event Handlers are fired more than once

I'm working with the MS UIA framework and its built-in Automation event handling. I'm seeing unexpected behavior on the WindowClosedEvent.

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.

[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
    }

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.

The solution problem is we found that in return to event call it return a value

   ( AutomationPropertyChangedEventArgs e)

the above event have property value that is passed to function so

           private void Property_Change_Event(object src, AutomationPropertyChangedEventArgs e)
    {
        AutomationElement sourceElement = src as AutomationElement;
        if (   e.Property  ==   WindowPatternIdentifiers.WindowClosedEvent)
        {
              //manage e.NewValue == 0 or  1

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM