您的位置:首页 > 编程语言 > PHP开发

How to execute tons of tasks parallelly with TPL method?

2015-11-29 17:12 603 查看
List<Task> taskList = new List<Task>();

// string currentNoStr = null; cannot define at here, we should define the copy in for loop, this is a  .NET FW bug for 4.0/4.5
// I am not sure whether this issue is fixed in latest version, below is the workaround
for (int currentNo = startPhaseNo; currentNo <= endPhaseNo; currentNo++)
{
string currentNoStr = currentNo.ToString();
taskList.Add(Task.Run(() =>
{
string result = GetLotteryByPhase(currentNoStr);
if (!string.IsNullOrEmpty(result))
{
resultDict.Add(currentNoStr, result);
}
}));

if (currentNo % 1000 >= 155)
{
currentNo /= 1000;
currentNo++;
currentNo *= 1000;
continue;
}
}
await Task.WhenAll(taskList);
return resultDict;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: