in C# I currently want to add an event which will fire when any window gets closed (not only a WPF or forms window). I got that step so far and that works perfectly fine. The SubscribeWindowClose
event will be added when another program starts.
I had a look at Microsoft's documentation about the Automation.AddAutomationEventHandler.
In the doc was a description about caching the AutomationElement which I didn't quite get.
I just want to compare the closed window title with window titles I've got in a list (List).
Can anyone help me with this or show me a better way to solve my "issue"?
Thanks in advance!
Code:
private void SubscribeWindowClose(AutomationElement window) {
Automation.AddAutomationEventHandler(WindowPattern.WindowClosedEvent,
window, TreeScope.Element, OnWindowClose);
}
private void OnWindowClose(object src, AutomationEventArgs e) {
try {
var element = src as AutomationElement;
Debug.WriteLine(element);
if (e.EventId == WindowPattern.WindowClosedEvent) {
// if window title is in List, run method
return;
}
} catch (ElementNotAvailableException) {
return;
}
}