C# 为什么搜索一个细胞这么慢?
我在Winform应用程序中有一个网格(FlexGrid,来自ComponentOne),我试图在该网格中找到一个单元格,给定该单元格的列索引及其值 我已经编写了下面的扩展方法来循环遍历网格并找到该单元格 我正在一个有6列64行的网格上测试这个方法。我的代码花了10分钟才找到正确的单元格(位于最后一行) 有什么方法可以加快我的算法吗 注意:我还尝试将PlayBack.PlayBackSetting.SmartMatchOption设置为TopLevelWindow,但它似乎没有改变任何东西 谢谢C# 为什么搜索一个细胞这么慢?,c#,winforms,performance,visual-studio-2010,coded-ui-tests,C#,Winforms,Performance,Visual Studio 2010,Coded Ui Tests,我在Winform应用程序中有一个网格(FlexGrid,来自ComponentOne),我试图在该网格中找到一个单元格,给定该单元格的列索引及其值 我已经编写了下面的扩展方法来循环遍历网格并找到该单元格 我正在一个有6列64行的网格上测试这个方法。我的代码花了10分钟才找到正确的单元格(位于最后一行) 有什么方法可以加快我的算法吗 注意:我还尝试将PlayBack.PlayBackSetting.SmartMatchOption设置为TopLevelWindow,但它似乎没有改变任何东西 谢谢
公共静态WinCell FindCellByColumnAndValue(此WinTable表,int-colIndex,字符串strCellValue)
{
int count=table.GetChildren().count;
对于(int-rowIndex=0;rowIndex
编辑
我修改了我的方法如下(例如,我不再使用winrow),这似乎快了3倍左右。在一个有3行6列的表中找到一个单元格仍然需要7秒,所以速度仍然很慢
稍后我会将这个答案标记为已被接受,以便留出时间让其他人提出更好的建议
public static WinCell FindCellByColumnAndValue(this WinTable table, int colIndex, string strCellValue, bool searchHeader = false)
{
Playback.PlaybackSettings.SmartMatchOptions = Microsoft.VisualStudio.TestTools.UITest.Extension.SmartMatchOptions.None;
int count = table.GetChildren().Count;
for (int rowIndex = 0; rowIndex < count; rowIndex++)
{
WinCell cell = new WinCell(table);
cell.SearchProperties.Add(WinRow.PropertyNames.RowIndex, rowIndex.ToString());
cell.SearchProperties.Add(WinCell.PropertyNames.ColumnIndex, colIndex.ToString());
cell.SearchProperties.Add(WinCell.PropertyNames.Value, strCellValue);
if (cell.Exists)
return cell;
}
return new WinCell();
}
我建议通过子控件执行直接循环-根据我的经验,在编码UI中使用复杂的搜索条件搜索控件通常运行缓慢 编辑: 为了提高性能,最好删除计数表的子项并遍历行。另外,为了避免在查找不带行号的控件时出现异常,可以使用方法,如下所示:
public static WinCell FindCellByColumnAndValue(this WinTable table, int colIndex, string strCellValue, bool searchHeader = false)
{
Playback.PlaybackSettings.SmartMatchOptions = Microsoft.VisualStudio.TestTools.UITest.Extension.SmartMatchOptions.None;
WinCell cell = new WinCell(table);
cell.SearchProperties.Add(WinCell.PropertyNames.ColumnIndex, colIndex.ToString());
cell.SearchProperties.Add(WinCell.PropertyNames.Value, strCellValue);
UITestControlCollection foundControls = cell.FindMatchingControls();
if (foundControls.Count > 0)
{
cell = foundControls.List[0];
}
else
{
cell = null;
}
return cell;
}
当直接在表中搜索表字段时,将节省计算表中子控件的时间。另外,在不使用的情况下搜索循环将为不匹配的行号的每次迭代节省搜索字段的时间
因为行号是通过扩展中的所有可用值进行迭代的,所以从长远来看,它不是必要的搜索条件。同时,通过行数值进行的每次迭代都会调用额外的控制搜索请求—最终将方法的执行时间乘以网格中的行数。假设您的意思是使用table.GetChildren(),这样对我来说就更慢了:(是的,我已经尝试过了,但是我得到了以下错误系统。ArgumentException:没有将任何行指定为控件的搜索容器。若要使用“ColumnIndex”搜索单元格控件,必须将行指定为容器元素,或将“RowIndex”添加到单元格的搜索属性中。参数名称:SearchProperties这就是为什么我要仍在使用rows@David您是否尝试仅使用指定的行实例进行搜索-而不使用特定的行号?@David似乎应该使用方法来查找没有行号的字段。我使用FindMatchingControl更新了我的原始帖子。正如我前面提到的,在searc时,您需要指定行容器或行索引ColumnIndexI的一个单元会同意,在MSDN论坛的某个部分,他们通常会为编码的UI问题提供相当广泛的答案。@leppie:是的,我在他们的论坛上发布了一条消息,我只是在等待answer@Andrii当前位置我会在那里碰碰运气,谢谢
public static WinCell FindCellByColumnAndValue(this WinTable table, int colIndex, string strCellValue, bool searchHeader = false)
{
Playback.PlaybackSettings.SmartMatchOptions = Microsoft.VisualStudio.TestTools.UITest.Extension.SmartMatchOptions.None;
WinCell cell = new WinCell(table);
cell.SearchProperties.Add(WinCell.PropertyNames.ColumnIndex, colIndex.ToString());
cell.SearchProperties.Add(WinCell.PropertyNames.Value, strCellValue);
UITestControlCollection foundControls = cell.FindMatchingControls();
if (foundControls.Count > 0)
{
cell = foundControls.List[0];
}
else
{
cell = null;
}
return cell;
}