PopulateTable(DataTable filterTable, TableDef contextTableDef)
{
string friendlyFieldName = "";
string friendlyTableName = "";
string friendlyFullName = "";
try
{
*For loop commented*
//foreach (FieldDef field in contextTableDef.Fields) {
//friendlyTableName = contextTableDef.Description;
//friendlyFieldName = field.FriendlyName;
//friendlyFullName = friendlyTableName + "." + friendlyFieldName;
//if (!field.CurrentView.TimeField) {
//filterTable.Rows.Add(contextTableDef.Name, friendlyFullName, //contextTableDef.Name, field.Name, friendlyFieldName, friendlyTableName);
//}
Parallel.ForEach(contextTableDef.Fields, (field) =>
{
friendlyTableName = contextTableDef.Description;
friendlyFieldName = field.FriendlyName;
friendlyFullName = friendlyTableName + "." + friendlyFieldName;
if (!field.CurrentView.TimeField) {
filterTable.Rows.Add(contextTableDef.Name, friendlyFullName, contextTableDef.Name, field.Name, friendlyFieldName, friendlyTableName);
}
});
foreach (TableDef childTable in contextTableDef.Children) {
if (childTable.IsOneToOne) {
PopulateFilterTable(filterTable, childTable);
}
}
return filterTable;
} catch (Exception ex) {
throw;
}
}
这里使用Parallel.Foreach而不是for循环来提高性能。在代码中我使用自定义集合TableDef,FiledDef。 当我运行代码并观察到循环已完成但应用程序被挂起时无法识别为什么会如此。