I have successfully used the solution for detecting opening windows described here: UIAutomation FlaUI - Detect opening windows on application level.
However, I would also like to also detect closing window events in addition to opening window events.
I duplicated the code in order to detect the second event:
AutomationElement root = automation.GetDesktop();
UIA3AutomationEventHandler automationOpenEventHandler = new UIA3AutomationEventHandler(
root.FrameworkAutomationElement,
automation.EventLibrary.Window.WindowOpenedEvent,
HandleOpenEvents);
automation.NativeAutomation.AddAutomationEventHandler(
automation.EventLibrary.Window.WindowOpenedEvent.Id,
root.ToNative(),
(Interop.UIAutomationClient.TreeScope)TreeScope.Descendants,
null,
automationOpenEventHandler);
UIA3AutomationEventHandler automationCloseEventHandler = new UIA3AutomationEventHandler(
root.FrameworkAutomationElement,
automation.EventLibrary.Window.WindowClosedEvent,
HandleCloseEvents);
automation.NativeAutomation.AddAutomationEventHandler(
automation.EventLibrary.Window.WindowClosedEvent.Id,
root.ToNative(),
(Interop.UIAutomationClient.TreeScope)TreeScope.Descendants,
null,
automationCloseEventHandler);
However, only one of the two (I believe the second one declared above) actually works. Note that if I declare just one of the handlers above, either the open or close, it works fine. I just don't know how to get both going.
Any help would be appreciated!