我们一直在使用 MS UIA 框架,并注意到在 Windows 10/.NET 4.6 中查找对象集合时速度似乎显着下降。
在 Windows 10/.NET 4.6 机器上测试 AutomationElement.FindAll() 时,我们查找元素集合的时间平均比在 Windows 8.1/.NET 4.5 上查找完全相同的元素时长大约 3 到 5 倍.1 箱。 我的测试是针对启用了虚拟化(使用回收)并获取 DataGrid 每一行内的所有单元格的 WPF DataGrid。
在我们的 Win10 机器上,每次 FindAll 调用以获取每行中的单元格大约需要 30 - 50 毫秒甚至更长的时间。在 Win8.1 机器上大约需要 5 - 10 毫秒。我无法弄清楚原因,但我不认为问题仅限于 DataGrid,因为我们的 FindAll() 调用没有什么特别之处。
//get grid
AutomationElement gridElement = AutomationElement.RootElement.FindFirst(TreeScope.Descendants,
new PropertyCondition(AutomationElement.AutomationIdProperty, "dataGridAutomationId"));
//get all visible rows
AutomationElementCollection dataItems = gridElement.FindAll(TreeScope.Descendants,
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.DataItem));
foreach (AutomationElement dataItem in dataItems)
{
if (!string.IsNullOrEmpty(dataItem.Current.Name))
{
//call under test
AutomationElementCollection cells = dataItem.FindAll(TreeScope.Children,
new PropertyCondition(AutomationElement.ClassNameProperty, "DataGridCell"));
}
}
AutomationElement.RootElement 的使用仅用于测试。 “dataItem.FindAll()”调用是我正在测试的。
Win 8.1 和 Win10 机器规范:
- 至强 W3670 3.20GHz 处理器
- 12GB内存
- 64 位操作系统
我们已尝试通过 com 包装使用非托管 MS uia API,但在 Win10 上没有发现明显的性能改进。
如有任何建议,我们将不胜感激。
最佳答案
这似乎已在最新一轮的 Windows 10 更新 (kb/3093266) 中得到修复。据我采访过的 MS 支持代表说:
“UIA 频繁调用 NtQuerySystemInformation,频繁调用该 API 的性能并不令人满意。他们对特定代码路径进行了更改,不再调用该 API,从而提高了整体性能。”
不幸的是,这就是他们所掌握的所有信息,所以我无法确定是那个电话导致了问题的确切原因。
更新和测试后,两台机器的性能相同。
关于c# - AutomationElement.FindAll() 性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32970247/