I'm working with application window containing a lot of log messages. I need to filter them and retrieve those only that mathches some condition. My choice to traverse them all is TreeWalker
as filtering full bulk of messages after AutomationElement.GetAll()
is too expensive (there might be thousands of messages).
List<AutomationElement> messages = new List<AutomationElement>();
TreeWalker walker = new TreeWalker(new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.DataItem));
AutomationElement row = walker.GetFirstChild(parentDatagrid);
while (row != null)
{
if (/*some condition*/)
messages.Add(row);
row = walker.GetNextSibling(row);
}
Here is the UISpy view of control hierarchy I'm testing.
Unexpectedly messages
length is larger that actual log messages count. I queried for extra automation elements is UISpy and found that this elements were retrieved from another window (they also matched the condition ControlTypeProperty = ControlType.DataItem
). Moreover, this window belonged even to another application. TreeWalker
finished its search in the scope of parentDatagrid
and continued to traverse all desktop hierarchy.
Of course, I hoped to get only child elements of datagrid. What could cause such strange TreeWalker
behaviour? Maybe, my code is wrong, but I wrote the same snipped multiple times, and it worked correctly.