Marcel Bonzelet Marcel Bonzelet - 3 months ago 27
C# Question

AutomationElement is still alive?

I am using the Microsoft UI Automation and have some problems with it, one being that I want to know if an AutomationElement is still alive. More precisely I want to check if a window has been closed. I think this is the most common case for this kind of question and I tried different scenarios, ending up with a solution where I try to access different properties of the element and see if it throws an ElementNotAvailableException. I also stumbled upon a property called IsOffscreen, which seems to be very helpful in this case. But still, as I couldn't find too much about this on the net, I want to know if there is a better solution. I haven't been too happy with the framework these last days because it seems very unstable to me (especially in finding an AutomationElement). Maybe you could help me get a little more expertise in my implementation.

Thank you very much

Marcel

Answer

Before fetching AutomationElement, you may catch the ElementNotAvailableException

try 
{
  var info = automationElement.Current;
  var name = info.Name;
}
catch (ElementNotAvailableException) {}