关键逻辑:Task.WaitAll(tasks.ToArray()); 等所有异步任务执行完以后,一块返回
使用例子如下:
public bool GetTaskData()
{
List<Task> tasks = new List<Task>();
Task<int> SaveContractTask = null;
Task<int> SavePaymentTask = null;
Task<int> SaveIncometask = null;
Task<int> SaveContractChangeInfoTask = null;
Task<int> SaveDSFTask = null;
SaveContractTask = new Task<int>(() =>
{
return _businessDataRepository.SaveBIContractInfos(biModel.ContractInfos);
});
SaveContractTask.Start();
tasks.Add(SaveContractTask);
SavePaymentTask = new Task<int>(() =>
{
return _businessDataRepository.SaveBIPaymentSchedule(biModel.PaymentSchedule);
});
SavePaymentTask.Start();
tasks.Add(SavePaymentTask);
SaveIncometask = new Task<int>(() =>
{
return _businessDataRepository.SaveBIIncomeSchedule(biModel.IncomeSchedule);
});
SaveIncometask.Start();
tasks.Add(SaveIncometask);
SaveContractChangeInfoTask = new Task<int>(() =>
{
return _businessDataRepository.SaveBIContractChangeInfos(biModel.ContractChangeInfos);
});
SaveContractChangeInfoTask.Start();
tasks.Add(SaveContractChangeInfoTask);
SaveDSFTask = new Task<int>(() =>
{
return _businessDataRepository.SaveBIDSFInfos(biModel.DSFInfos);
});
SaveDSFTask.Start();
tasks.Add(SaveDSFTask);
if (tasks.Count > 0)
{
Task.WaitAll(tasks.ToArray());
}
return true;
}