如何使方法调用异步

时间:2017-11-17 12:16:47

标签: asynchronous c#-4.0 async-await dataset

我有大约30个方法调用(填充数据集)。我想让这些调用并行(异步)。 请给我一些代码的想法。以下是一些电话。

 ds = (DS)dbManager.ExecuteDataSet(CommandType.StoredProcedure, Proc1, ds, tableNames);
 ds = (DS)dbManager.ExecuteDataSet(CommandType.StoredProcedure, Proc2, ds, tableNames);
 ds = (DS)dbManager.ExecuteDataSet(CommandType.StoredProcedure, Proc3, ds, tableNames);
 ds = (DS)dbManager.ExecuteDataSet(CommandType.StoredProcedure, Proc4, ds, tableNames);
 ds = (DS)dbManager.ExecuteDataSet(CommandType.StoredProcedure, Proc5, ds, tableNames);
 ds = (DS)dbManager.ExecuteDataSet(CommandType.StoredProcedure, Proc6, ds, tableNames);
 ds = (DS)dbManager.ExecuteDataSet(CommandType.StoredProcedure, Proc7, ds, tableNames);
 ds = (DS)dbManager.ExecuteDataSet(CommandType.StoredProcedure, Proc8, ds, tableNames);

1 个答案:

答案 0 :(得分:1)

从我的理解你的Proc1,proc2 ..是程序名称。只需将它们加载到列表中并尝试:

string[] procNames = {"Proc1","Proc2","Proc3","Proc4","Proc5"};
List<string> list =new List<string>();
foreach (var procName in procNames)
{
    list.Add(procName);
}
Parallel.ForEach(list, current =>
{
    ds = (DS)dbManager.ExecuteDataSet(CommandType.StoredProcedure, current, ds, tableNames);
});

请注意,这不会以任何特定顺序执行您的程序。