C# Task.Parallel


此示例演示了使用多种语言构造实现并行循环的几种方法。
 1 using System.Threading.Tasks;   
 2 class Test
 3 {
 4     static int N = 1000;
 5 
 6     static void TestMethod()
 7     {
 8         // Using a named method.
 9         Parallel.For(0, N, Method2);
10 
11         // Using an anonymous method.
12         Parallel.For(0, N, delegate(int i)
13         {
14             // Do Work.
15         });
16 
17         // Using a lambda expression.
18         Parallel.For(0, N, i =>
19         {
20             // Do Work.
21         });
22     }
23 
24     static void Method2(int i)
25     {
26         // Do work.
27     }
28 }

也有其他的循环

For(Int32, Int32, Action)

执行 for 循环,其中可能会并行运行迭代,而且可以监视和操作循环的状态。

For(Int32, Int32, Action)

执行 for 循环,其中可能会并行运行迭代。

For(Int32, Int32, ParallelOptions, Action)

执行 for 循环,其中可能会并行运行迭代,而且可以配置循环选项,可以监视和操作循环的状态。

For(Int32, Int32, ParallelOptions, Action)

执行 for 循环,其中可能会并行运行迭代,而且可以配置循环选项。

For(Int64, Int64, Action)

执行具有 64 位索引的 for 循环,其中可能会并行运行迭代,而且可以监视和操作循环的状态。

For(Int64, Int64, Action)

执行具有 64 位索引的 for 循环,其中可能会并行运行迭代,而且可以监视和操作循环的状态。

For(Int64, Int64, ParallelOptions, Action)

执行具有 64 位索引的 for 循环,其中可能会并行运行迭代,而且可以配置循环选项,可以监视和操作循环的状态。

For(Int64, Int64, ParallelOptions, Action)

执行具有 64 位索引的 for 循环,其中可能会并行运行迭代,而且可以配置循环选项。

For(Int32, Int32, Func, Func, Action)

执行具有线程本地数据的 for 循环,其中可能会并行运行迭代,而且可以监视和操作循环的状态。

For(Int32, Int32, ParallelOptions, Func, Func, Action)

执行具有线程本地数据的 for 循环,其中可能会并行运行迭代,而且可以配置循环选项,可以监视和操作循环的状态。

For(Int64, Int64, Func, Func, Action)

执行具有 64 位索引和线程本地数据的 for 循环,其中可能会并行运行迭代,而且可以监视和操作循环的状态。

For(Int64, Int64, ParallelOptions, Func, Func, Action)

执行具有 64 位索引和线程本地数据的 for 循环,其中可能会并行运行迭代,而且可以配置循环选项,可以监视和操作循环的状态。

ForEach(IEnumerable, Func, Func, Action)

执行具有线程本地数据的 foreach(在 Visual Basic 中为 For Each)操作,其中在 IEnumerable 上可能会并行运行迭代,而且可以监视和操作循环的状态。

ForEach(IEnumerable, Func, Func, Action)

执行具有线程本地数据的 foreach(在 Visual Basic 中为 For Each)操作,其中在 IEnumerable 上可能会并行运行迭代,而且可以监视和操作循环的状态。

ForEach(IEnumerable, ParallelOptions, Func, Func, Action)

执行具有线程本地数据和 64 位索引的 foreach(在 Visual Basic 中为 For Each)操作,其中在 IEnumerable 上可能会并行运行迭代,而且可以配置循环选项,可以监视和操作循环的状态。

ForEach(IEnumerable, ParallelOptions, Func, Func, Action)

执行具有线程本地数据的 foreach(在 Visual Basic 中为 For Each)操作,其中在 IEnumerable 上可能会并行运行迭代,而且可以配置循环选项,可以监视和操作循环的状态。

ForEach(OrderablePartitioner, Func, Func, Action)

执行具有线程本地数据的 foreach(在 Visual Basic 中为 For Each)操作,其中在 OrderablePartitioner 上可能会并行运行迭代,而且可以配置循环选项,可以监视和操作循环的状态。

ForEach(OrderablePartitioner, ParallelOptions, Func, Func, Action)

执行具有 64 位索引和线程本地数据的 foreach(在 Visual Basic 中为 For Each)操作,其中在 OrderablePartitioner 上可能会并行运行迭代,而且可以配置循环选项,可以监视和操作循环的状态。

ForEach(Partitioner, Func, Func, Action)

执行具有线程本地数据的 foreach(在 Visual Basic 中为 For Each)操作,其中在 Partitioner 上可能会并行运行迭代,而且可以监视和操作循环的状态。

ForEach(Partitioner, ParallelOptions, Func, Func, Action)

执行具有线程本地数据的 foreach(在 Visual Basic 中为 For Each)操作,其中在 Partitioner 上可能会并行运行迭代,而且可以配置循环选项,可以监视和操作循环的状态。

ForEach(IEnumerable, Action)

执行具有 64 位索引的 foreach(在 Visual Basic 中为 For Each)操作,其中在 IEnumerable 上可能会并行运行迭代,而且可以监视和操作循环的状态。

ForEach(IEnumerable, Action)

执行 foreach(在 Visual Basic 中为 For Each)操作,其中在 IEnumerable 中可能会并行运行迭代,而且可以监视和操作循环的状态。

ForEach(IEnumerable, Action)

执行 foreach(在 Visual Basic 中为 For Each)操作,其中在 IEnumerable 上可能会并行运行迭代。

ForEach(IEnumerable, ParallelOptions, Action)

执行具有 64 位索引的 foreach(在 Visual Basic 中为 For Each)操作,其中在 IEnumerable 上可能会并行运行迭代,而且可以配置循环选项,可以监视和操作循环的状态。

ForEach(IEnumerable, ParallelOptions, Action)

执行 foreach(在 Visual Basic 中为 For Each)操作,其中在 IEnumerable 中可能会并行运行迭代,而且可以配置循环选项,可以监视和操作循环的状态。

ForEach(IEnumerable, ParallelOptions, Action)

执行 foreach(在 Visual Basic 中为 For Each)操作,其中在 IEnumerable 中可能会并行运行迭代,而且可以匹配配置循环选项。

ForEach(OrderablePartitioner, Action)

执行 foreach(在 Visual Basic 中为 For Each)操作,其中在 OrderablePartitioner 中可能会并行运行迭代,而且可以监视和操作循环的状态。

ForEach(OrderablePartitioner, ParallelOptions, Action)

执行 foreach(在 Visual Basic 中为 For Each)操作,其中在 OrderablePartitioner 中可能会并行运行迭代,而且可以配置循环选项,可以监视和操作循环的状态。

ForEach(Partitioner, Action)

执行 foreach(在 Visual Basic 中为 For Each)操作,其中在 Partitioner 中可能会并行运行迭代,而且可以监视和操作循环的状态。

ForEach(Partitioner, Action)

执行 foreach(在 Visual Basic 中为 For Each)操作,其中在 Partitioner 上可能会并行运行迭代。

ForEach(Partitioner, ParallelOptions, Action)

执行 foreach(在 Visual Basic 中为 For Each)操作,其中在 Partitioner 中可能会并行运行迭代,而且可以配置循环选项,可以监视和操作循环的状态。

ForEach(Partitioner, ParallelOptions, Action)

执行 foreach(在 Visual Basic 中为 For Each)操作,其中在 Partitioner 中可能会并行运行迭代,而且可以配置循环选项。

ForEachAsync(IAsyncEnumerable, CancellationToken, Func)

对迭代并行运行 的每个 操作执行一个 IEnumerable

ForEachAsync(IAsyncEnumerable, Func)

对迭代并行运行 的每个 操作执行一个 IEnumerable

ForEachAsync(IAsyncEnumerable, ParallelOptions, Func)

对迭代并行运行 的每个 操作执行一个 IEnumerable

ForEachAsync(IEnumerable, CancellationToken, Func)

对迭代并行运行 的每个 操作执行一个 IEnumerable

ForEachAsync(IEnumerable, Func)

对迭代并行运行 的每个 操作执行一个 IEnumerable

ForEachAsync(IEnumerable, ParallelOptions, Func)

对迭代并行运行 的每个 操作执行一个 IEnumerable

Invoke(Action[])

尽可能并行执行提供的每个操作。

Invoke(ParallelOptions, Action[])

执行所提供的每个操作,而且尽可能并行运行,除非用户取消了操作。

常用的几个方法:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

    class ParallelInvokeDemo
    {

        // Demonstrated features:
        //         Parallel.Invoke()
        // Expected results:
        //         The threads on which each task gets executed may be different.
        //        The thread assignments may be different in different executions.
        //        The tasks may get executed in any order.
        // Documentation:
        //        http://msdn.microsoft.com/library/dd783942(VS.100).aspx
        static void Main()
        {
            try
            {
                Parallel.Invoke(
                    BasicAction,    // Param #0 - static method
                    () =>            // Param #1 - lambda expression
                    {
                        Console.WriteLine("Method=beta, Thread={0}", Thread.CurrentThread.ManagedThreadId);
                    },
                    delegate()        // Param #2 - in-line delegate
                    {
                        Console.WriteLine("Method=gamma, Thread={0}", Thread.CurrentThread.ManagedThreadId);
                    }
                );
            }
            // No exception is expected in this example, but if one is still thrown from a task,
            // it will be wrapped in AggregateException and propagated to the main thread.
            catch (AggregateException e)
            {
                Console.WriteLine("An action has thrown an exception. THIS WAS UNEXPECTED.\n{0}", e.InnerException.ToString());
            }
        }

        static void BasicAction()
        {
            Console.WriteLine("Method=alpha, Thread={0}", Thread.CurrentThread.ManagedThreadId);
        }
    }