c# - Automation Event Handlers are fired more than once -


i'm working ms uia framework , built-in automation event handling. i'm seeing unexpected behavior on windowclosedevent.

perhaps i'm doing wrong here, understanding console call should executed once. when run above code console line executed twice.

when testapp close there no special events runs. test app single wpf window.

[testmethod]     public void testmethod1()     {         process.start(@"testapp.exe");          thread.sleep(2000); //sleep wait proc boot 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 wait uia's event threads fire , finish.     }      public void onclose(object sender, automationeventargs e)     {         console.writeline("test");  //this run twice during test     } 

i'm running against .net 4.5.2 on windows 10.

if i'm doing wrong here cause handler fire more once please let me know.

thanks.

the solution problem found in return event call return value

   ( automationpropertychangedeventargs e) 

the above event have property value passed function so

           private void property_change_event(object src, automationpropertychangedeventargs e)     {         automationelement sourceelement = src automationelement;         if (   e.property  ==   windowpatternidentifiers.windowclosedevent)         {               //manage e.newvalue == 0 or  1          }