I'm working on a project where I need to get the name of windows which is being closed. I'm using C# Automation events for this.
I've pasted below the code that I'm using:
Automation.AddAutomationEventHandler(WindowPattern.WindowClosedEvent,
AutomationElement.RootElement, TreeScope.Subtree, (sender, eve) =>
{
AutomationElement winElemnt = sender as AutomationElement;
if (winElemnt != null)
{
Console.WriteLine("Window closed : " + winElemnt.Current.Name);
}
});
The above code get triggered when any window closes but I'm getting following error on execution:
- Value of variable sender will be null.
- Unable to get automation element's Current.Name (I'm getting this error most of the time)
On debugging I was able to find that the 2nd error is due to window closing before completing event handler execution.
Please let me know how to fix these errors and get the name of window for which close is triggered.
Thanks in advance