#region 用限定线程数量,完成多任务
int taskCount = 10000;
List<int> list = new List<int>();
for (int i = 0; i < taskCount; i++)
{
list.Add(i);
}
Action<int> action = i =>
{
//任务内容 封装
};
List<Task> tasklist = new List<Task>();
foreach (var item in list)
{
int k = item;
tasklist.Add(Task.Run(() => action.Invoke(k)));
if (tasklist.Count > 10)
{
Task.WaitAny(tasklist.ToArray());
tasklist = tasklist.Where(t => t.Status != TaskStatus.RanToCompletion).ToList();
}
}
Task.WhenAll(tasklist.ToArray());