码迷,mamicode.com
首页 > 其他好文 > 详细

How to execute tons of tasks parallelly with TPL method?

时间:2015-11-29 18:03:55      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:

            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;

 

How to execute tons of tasks parallelly with TPL method?

标签:

原文地址:http://www.cnblogs.com/researcher/p/5005028.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!