Is the AutomationElement still alive?

I am using Microsoft UI Automation and have some problems with it, one of which is that I want to know that the AutomationElement is still alive. More precisely, I want to check if the window is closed. I think this is the most common case for this kind of question, and I've tried different scenarios, the result is a solution where I try to access various properties of an element and see if it throws an ElementNotAvailableException. I also came across a property called IsOffscreen, which seems to be very useful in this case. But still, since I couldn't find too much about this on the net, I want to know if there is a better solution. I was not happy with the framework of these last days because it seems very unstable to me (especially when looking for AutomationElement). Maybe,could you help me gain a little more experience with my implementation.

Many thanks

Marseilles

+3


source to share


2 answers


Before retrieving AutomationElement you can catch ElementNotAvailableException



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

      

+1


source


I would wrap it in an extension:



public static bool Alive(this AutomationElement ae)
{
    try
    {
        var t = ae.Current.Name;
        return true;
    }
    catch(ElementNotAvailableException)
    {
        return false;
    }
}

      

+1


source







All Articles