How to execute tons of tasks parallelly with TPL method?

            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;
原文地址:https://www.cnblogs.com/researcher/p/5005028.html