Parallel 類別

定義

提供平行迴路與區域的支援。

public ref class Parallel abstract sealed
public static class Parallel
type Parallel = class
Public Class Parallel
繼承
Parallel

範例

此範例展示了使用多種語言結構實現平行迴圈的方法。

using System.Threading.Tasks;   
class Test
{
    static int N = 1000;

    static void TestMethod()
    {
        // Using a named method.
        Parallel.For(0, N, Method2);

        // Using an anonymous method.
        Parallel.For(0, N, delegate(int i)
        {
            // Do Work.
        });

        // Using a lambda expression.
        Parallel.For(0, N, i =>
        {
            // Do Work.
        });
    }

    static void Method2(int i)
    {
        // Do work.
    }
}
Imports System.Threading.Tasks

Module Module1

    Sub Main()
        Dim N As Integer = 1000

        ' Using a named method
        Parallel.For(0, N, AddressOf Method2)

        ' Using a lambda expression.
        Parallel.For(0, N, Sub(i)
                               ' Do Work
                           End Sub)
    End Sub

    Sub Method2(ByVal i As Integer)
        ' Do work.
    End Sub

End Module

備註

Parallel 類別提供基於函式庫的資料平行替換,用於常見操作,如 for loops、for each loops 以及執行一組語句。

方法

名稱 Description
For(Int32, Int32, Action<Int32,ParallelLoopState>)

執行 for 一個迴圈,其中可平行執行迭代,並可監控與操作迴圈狀態。

For(Int32, Int32, Action<Int32>)

執行 for 一個迴圈,使迭代可以平行執行。

For(Int32, Int32, ParallelOptions, Action<Int32,ParallelLoopState>)

執行 for 一個迴圈,在其中可平行執行迭代,可設定迴圈選項,並監控與操作迴圈狀態。

For(Int32, Int32, ParallelOptions, Action<Int32>)

執行 for 一個迴圈,該迴圈可平行執行迭代,並可配置迴圈選項。

For(Int64, Int64, Action<Int64,ParallelLoopState>)

執行 for 一個 64 位元索引的迴圈,該迴圈可平行執行,迴圈狀態可被監控與操作。

For(Int64, Int64, Action<Int64>)

執行 for 一個 64 位元索引的迴圈,並可平行執行迭代。

For(Int64, Int64, ParallelOptions, Action<Int64,ParallelLoopState>)

執行 for 一個 64 位元索引的迴圈,該迴圈可平行執行,迴圈選項可配置,迴圈狀態可被監控與操作。

For(Int64, Int64, ParallelOptions, Action<Int64>)

執行 for 一個 64 位元索引的迴圈,可平行迭代並配置迴圈選項。

For<TLocal>(Int32, Int32, Func<TLocal>, Func<Int32,ParallelLoopState,TLocal,TLocal>, Action<TLocal>)

執行 for 一個包含執行緒本地資料的迴圈,該迴圈可平行執行,迴圈狀態可被監控與操作。

For<TLocal>(Int32, Int32, ParallelOptions, Func<TLocal>, Func<Int32,ParallelLoopState,TLocal,TLocal>, Action<TLocal>)

執行 for 一個包含線程本地資料的迴圈,該迴圈可平行執行,迴圈選項可設定,迴圈狀態可被監控與操作。

For<TLocal>(Int64, Int64, Func<TLocal>, Func<Int64,ParallelLoopState,TLocal,TLocal>, Action<TLocal>)

執行 for 一個包含 64 位元索引與執行緒本地資料的迴圈,該迴圈可平行執行,迴圈狀態可被監控與操作。

For<TLocal>(Int64, Int64, ParallelOptions, Func<TLocal>, Func<Int64,ParallelLoopState,TLocal,TLocal>, Action<TLocal>)

執行 for 一個包含 64 位元索引與執行緒本地資料的迴圈,該迴圈可平行執行,迴圈選項可配置,迴圈狀態可被監控與操作。

ForEach<TSource,TLocal>(IEnumerable<TSource>, Func<TLocal>, Func<TSource,ParallelLoopState,Int64,TLocal,TLocal>, Action<TLocal>)

foreach 上執行 For Each(Visual Basic 中為 IEnumerable)操作,可平行迭代,並監控與操作迴圈狀態。

ForEach<TSource,TLocal>(IEnumerable<TSource>, Func<TLocal>, Func<TSource,ParallelLoopState,TLocal,TLocal>, Action<TLocal>)

在 Visual Basicforeach 上執行 For EachIEnumerable)操作,並可平行執行迴圈狀態。

ForEach<TSource,TLocal>(IEnumerable<TSource>, ParallelOptions, Func<TLocal>, Func<TSource,ParallelLoopState,Int64,TLocal,TLocal>, Action<TLocal>)

foreach 上執行 For Each(Visual Basic 中為 IEnumerable)操作,使用執行緒本地資料與 64 位元索引,該操作可平行執行,可配置迴圈選項,並監控與操作迴圈狀態。

ForEach<TSource,TLocal>(IEnumerable<TSource>, ParallelOptions, Func<TLocal>, Func<TSource,ParallelLoopState,TLocal,TLocal>, Action<TLocal>)

foreach 上執行 For Each(Visual Basic 中為 IEnumerable)操作,可平行迭代,可設定迴圈選項,並監控與操作迴圈狀態。

ForEach<TSource,TLocal>(OrderablePartitioner<TSource>, Func<TLocal>, Func<TSource,ParallelLoopState,Int64,TLocal,TLocal>, Action<TLocal>)

在 Visual Basicforeach 上執行 For EachOrderablePartitioner<TSource>)操作,並以執行緒本地資料,可平行執行迭代,可設定迴圈選項,並監控與操作迴圈狀態。

ForEach<TSource,TLocal>(OrderablePartitioner<TSource>, ParallelOptions, Func<TLocal>, Func<TSource,ParallelLoopState,Int64,TLocal,TLocal>, Action<TLocal>)

執行 foreach(Visual BasicFor Each)操作,採用 64 位元索引及執行緒本地資料,OrderablePartitioner<TSource> 執行緒本地資料,可平行執行迭代,可設定迴圈選項,並監控與操作迴圈狀態。

ForEach<TSource,TLocal>(Partitioner<TSource>, Func<TLocal>, Func<TSource,ParallelLoopState,TLocal,TLocal>, Action<TLocal>)

foreach 上執行 For Each(Visual Basic 中為 Partitioner)操作,該操作可平行迭代,並監控與操作迴圈狀態。

ForEach<TSource,TLocal>(Partitioner<TSource>, ParallelOptions, Func<TLocal>, Func<TSource,ParallelLoopState,TLocal,TLocal>, Action<TLocal>)

在 Visual Basicforeach 上執行 For EachPartitioner)操作,並以執行緒本地資料,可平行執行迭代,可設定迴圈選項,並監控與操作迴圈狀態。

ForEach<TSource>(IEnumerable<TSource>, Action<TSource,ParallelLoopState,Int64>)

foreach 上執行 For Each(Visual Basic 中為 IEnumerable)索引,可平行迭代,並監控與操作迴圈狀態。

ForEach<TSource>(IEnumerable<TSource>, Action<TSource,ParallelLoopState>)

執行一個 foreach(Visual Basic 中為 For Each)操作,IEnumerable 可平行執行,迴圈狀態可被監控與操作。

ForEach<TSource>(IEnumerable<TSource>, Action<TSource>)

執行一個foreach(Visual Basic 中為 For Each)操作,該操作可平行執行的 IEnumerable

ForEach<TSource>(IEnumerable<TSource>, ParallelOptions, Action<TSource,ParallelLoopState,Int64>)

foreach 上執行 For Each(Visual Basic 中為 IEnumerable)索引,可平行迭代,可設定迴圈選項,並監控與操作迴圈狀態。

ForEach<TSource>(IEnumerable<TSource>, ParallelOptions, Action<TSource,ParallelLoopState>)

對一個 foreach 執行 For Each(Visual Basic 中為 IEnumerable)操作,該操作可平行執行,可設定迴圈選項,並監控與操作迴圈狀態。

ForEach<TSource>(IEnumerable<TSource>, ParallelOptions, Action<TSource>)

對一個 foreach 執行 For Each(Visual Basic 中IEnumerable)操作,可平行迭代並配置迴圈選項。

ForEach<TSource>(OrderablePartitioner<TSource>, Action<TSource,ParallelLoopState,Int64>)

執行一個 foreach(Visual Basic 中為 For Each)操作,OrderablePartitioner<TSource> 可平行執行,且迴圈狀態可被監控與操作。

ForEach<TSource>(OrderablePartitioner<TSource>, ParallelOptions, Action<TSource,ParallelLoopState,Int64>)

對一個foreach執行For Each(Visual Basic 中OrderablePartitioner<TSource>)操作,該操作可平行執行,迴圈選項可配置,迴圈狀態可被監控與操作。

ForEach<TSource>(Partitioner<TSource>, Action<TSource,ParallelLoopState>)

foreach 上執行 For Each(Visual Basic 中為 Partitioner)操作,該操作可平行迭代,且迴圈狀態可被監控與操作。

ForEach<TSource>(Partitioner<TSource>, Action<TSource>)

執行一個 foreach(Visual Basic 中為 For Each)操作,該操作可平行執行的 Partitioner,且可平行執行迭代。

ForEach<TSource>(Partitioner<TSource>, ParallelOptions, Action<TSource,ParallelLoopState>)

對一個foreach執行For Each(Visual Basic 中Partitioner)操作,該操作可平行執行,迴圈選項可配置,迴圈狀態可被監控與操作。

ForEach<TSource>(Partitioner<TSource>, ParallelOptions, Action<TSource>)

foreach 上執行 For Each(Visual Basic 中為 Partitioner)操作,該操作可平行執行迭代並配置迴圈選項。

Invoke(Action[])

執行每個提供的動作,可能是並行執行。

Invoke(ParallelOptions, Action[])

執行每個提供的動作,可能並行執行,除非使用者取消該操作。

適用於

執行緒安全性

所有公開且受保護的成員 Parallel 皆為執行緒安全,且可同時從多個執行緒使用。

另請參閱