System.Threading.Tasks.Parallel是能够以并行的方式迭代数据集合(实现了IEnumerable<T>的对象),它主要提供2个方法:For()和ForEach()
事例:
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Net.ConsoleApplication
{
public class Student
{
public string id { get; set; }
public string name { get; set; }
}
public class ParallelDemo
{
private List<Student> list = new List<Student>();
public ParallelDemo()
{
for (int i = 0; i < 1000;i++ )
{
Student s = new Student();
s.id = i.ToString("00000");
s.name = "编号" + s.id;
list.Add(s);
}
}
public void Do()
{
Parallel.ForEach(list, stu => {
System.Console.WriteLine("id: " + stu.id + ",name: " + stu.name);
});
}
}
}
C# Parallel类的作用,布布扣,bubuko.com
原文:http://blog.csdn.net/y_f123/article/details/25454433