C#并行计算 Parallel.Foreach&Parallel.For

 

Parallel.For(int fromInclude, int toExclude, Action<int> body)

栗子:

Parallel.For(0, 10, (i) => {
    Console.Write(i);
});
Console.WriteLine();


Parallel.ForEach<T>(IEnumerable<T>, Action<T>)

栗子:

var all = new [] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Parallel.ForEach(all, (i) => {
    Console.Write($"{i} ");
});
Console.WriteLine();
 
posted @ 2017-04-21 16:41  gaozejie  阅读(3053)  评论(0编辑  收藏  举报