Parallel 類別

定義

提供平行迴圈和區域的支援。Provides support for parallel loops and regions.

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

範例

這個範例示範使用多個語言結構來執行平行迴圈的幾個方法。This example demonstrates several approaches to implementing a parallel loop using multiple language constructs.

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 迴圈)、針對每個迴圈,以及執行一組語句,提供以程式庫為基礎的資料平行取代。The Parallel class provides library-based data parallel replacements for common operations such as for loops, for each loops, and execution of a set of statements.

方法

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

執行 for 迴圈,其中可以平行執行反覆項目,還可以監控和操作迴圈的狀態。Executes a for loop in which iterations may run in parallel and the state of the loop can be monitored and manipulated.

For(Int32, Int32, Action<Int32>)

執行可平行執行反覆項目的 for 迴圈。Executes a for loop in which iterations may run in parallel.

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

執行 for 迴圈,其中可以平行執行反覆項目、可以設定迴圈選項,還可以監控和操作迴圈的狀態。Executes a for loop in which iterations may run in parallel, loop options can be configured, and the state of the loop can be monitored and manipulated.

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

執行 for 迴圈,其中可以平行執行反覆項目,還可以設定迴圈選項。Executes a for loop in which iterations may run in parallel and loop options can be configured.

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

使用 64 位元索引執行 for 迴圈,其中可以平行執行反覆項目,還可以監控和操作迴圈的狀態。Executes a for loop with 64-bit indexes in which iterations may run in parallel and the state of the loop can be monitored and manipulated.

For(Int64, Int64, Action<Int64>)

使用 64 位元索引執行 for 迴圈,其中可以平行執行反覆項目。Executes a for loop with 64-bit indexes in which iterations may run in parallel.

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

使用 64 位元索引執行 for 迴圈,其中可以平行執行反覆項目、可以設定迴圈選項,還可以監控和操作迴圈的狀態。Executes a for loop with 64-bit indexes in which iterations may run in parallel, loop options can be configured, and the state of the loop can be monitored and manipulated.

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

使用 64 位元索引執行 for 迴圈,其中可以平行執行反覆項目,還可以設定迴圈選項。Executes a for loop with 64-bit indexes in which iterations may run in parallel and loop options can be configured.

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

使用執行緒區域資料執行 for 迴圈,其中可以平行執行反覆項目,還可以監控和操作迴圈的狀態。Executes a for loop with thread-local data in which iterations may run in parallel, and the state of the loop can be monitored and manipulated.

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

使用執行緒區域資料執行 for 迴圈,其中可以平行執行反覆項目、可以設定迴圈選項,還可以監控和操作迴圈的狀態。Executes a for loop with thread-local data in which iterations may run in parallel, loop options can be configured, and the state of the loop can be monitored and manipulated.

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

使用 64 位元索引和執行緒區域資料執行 for 迴圈,其中可以平行執行反覆項目,還可以監控和操作迴圈的狀態。Executes a for loop with 64-bit indexes and thread-local data in which iterations may run in parallel, and the state of the loop can be monitored and manipulated.

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

使用 64 位元索引和執行緒區域資料執行 for 迴圈,其中可以平行執行反覆項目、可以設定迴圈選項,還可以監控和操作迴圈的狀態。Executes a for loop with 64-bit indexes and thread-local data in which iterations may run in parallel, loop options can be configured, and the state of the loop can be monitored and manipulated.

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

使用執行緒區域資料在 foreach 上執行 For Each (在 Visual Basic 中為 IEnumerable) 運算,其中可以平行執行反覆項目,還可以監控和操作迴圈的狀態。Executes a foreach (For Each in Visual Basic) operation with thread-local data on an IEnumerable in which iterations may run in parallel and the state of the loop can be monitored and manipulated.

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

使用執行緒區域資料在 foreach 上執行 For Each (在 Visual Basic 中為 IEnumerable) 運算,其中可以平行執行反覆項目,還可以監控和操作迴圈的狀態。Executes a foreach (For Each in Visual Basic) operation with thread-local data on an IEnumerable in which iterations may run in parallel, and the state of the loop can be monitored and manipulated.

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

使用執行緒區域資料和 64 位元索引在 foreach 上執行 For Each (在 Visual Basic 中為 IEnumerable) 運算,其中可以平行執行反覆項目、可以設定迴圈選項,還可以監控和操作迴圈的狀態。Executes a foreach (For Each in Visual Basic) operation with thread-local data and 64-bit indexes on an IEnumerable in which iterations may run in parallel, loop options can be configured, and the state of the loop can be monitored and manipulated.

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

使用執行緒區域資料,在 foreach 上執行 For Each (Visual Basic 中的 IEnumerable) 作業,其中可以平行執行反覆運算、可設定迴圈選項,且可以監視和操作迴圈狀態。Executes a foreach (For Each in Visual Basic) operation with thread-local data on an IEnumerable in which iterations may run in parallel, loop options can be configured, and the state of the loop can be monitored and manipulated.

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

使用執行緒區域資料在 foreach 上執行 For Each (在 Visual Basic 中為 OrderablePartitioner<TSource>) 運算,其中可以平行執行反覆項目、可以設定迴圈選項,還可以監控和操作迴圈的狀態。Executes a foreach (For Each in Visual Basic) operation with thread-local data on a OrderablePartitioner<TSource> in which iterations may run in parallel, loop options can be configured, and the state of the loop can be monitored and manipulated.

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

使用 64 位元索引和執行緒區域資料在 foreach 上執行 For Each (在 Visual Basic 中為 OrderablePartitioner<TSource>) 運算,其中可以平行執行反覆項目、可以設定迴圈選項,還可以監控和操作迴圈的狀態。Executes a foreach (For Each in Visual Basic) operation with 64-bit indexes and with thread-local data on a OrderablePartitioner<TSource> in which iterations may run in parallel , loop options can be configured, and the state of the loop can be monitored and manipulated.

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

使用執行緒區域資料在 foreach 上執行 For Each (在 Visual Basic 中為 Partitioner) 運算,其中可以平行執行反覆項目,還可以監控和操作迴圈的狀態。Executes a foreach (For Each in Visual Basic) operation with thread-local data on a Partitioner in which iterations may run in parallel and the state of the loop can be monitored and manipulated.

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

使用執行緒區域資料在 foreach 上執行 For Each (在 Visual Basic 中為 Partitioner) 運算,其中可以平行執行反覆項目、可以設定迴圈選項,還可以監控和操作迴圈的狀態。Executes a foreach (For Each in Visual Basic) operation with thread-local data on a Partitioner in which iterations may run in parallel, loop options can be configured, and the state of the loop can be monitored and manipulated.

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

使用 64 位元索引在 foreach 上執行 For Each (在 Visual Basic 中為 IEnumerable) 運算,其中可以平行執行反覆項目,還可以監控和操作迴圈的狀態。Executes a foreach (For Each in Visual Basic) operation with 64-bit indexes on an IEnumerable in which iterations may run in parallel, and the state of the loop can be monitored and manipulated.

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

foreach 上執行 For Each (在 Visual Basic 中為 IEnumerable) 運算,其中可以平行執行反覆項目,還可以監控和操作迴圈的狀態。Executes a foreach (For Each in Visual Basic) operation on an IEnumerable in which iterations may run in parallel, and the state of the loop can be monitored and manipulated.

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

foreach 上執行 For Each (在 Visual Basic 中為 IEnumerable) 運算,其中可以平行執行反覆項目。Executes a foreach (For Each in Visual Basic) operation on an IEnumerable in which iterations may run in parallel.

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

使用 64 位元索引在 foreach 上執行 For Each (在 Visual Basic 中為 IEnumerable) 運算,其中可以平行執行反覆項目、可以設定迴圈選項,還可以監控和操作迴圈的狀態。Executes a foreach (For Each in Visual Basic) operation with 64-bit indexes on an IEnumerable in which iterations may run in parallel, loop options can be configured, and the state of the loop can be monitored and manipulated.

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

foreach 上執行 For Each (在 Visual Basic 中為 IEnumerable) 運算,其中可以平行執行反覆項目、可以設定迴圈選項,還可以監控和操作迴圈的狀態。Executes a foreach (For Each in Visual Basic) operation on an IEnumerable in which iterations may run in parallel, loop options can be configured, and the state of the loop can be monitored and manipulated.

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

foreach 上執行 For Each (在 Visual Basic 中為 IEnumerable) 運算,其中可以平行執行反覆項目,還可以設定迴圈選項。Executes a foreach (For Each in Visual Basic) operation on an IEnumerable in which iterations may run in parallel and loop options can be configured.

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

foreach 上執行 For Each (在 Visual Basic 中為 OrderablePartitioner<TSource>) 運算,其中可以平行執行反覆項目,還可以監控和操作迴圈的狀態。Executes a foreach (For Each in Visual Basic) operation on a OrderablePartitioner<TSource> in which iterations may run in parallel and the state of the loop can be monitored and manipulated.

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

foreach 上執行 For Each (在 Visual Basic 中為 OrderablePartitioner<TSource>) 運算,其中可以平行執行反覆項目、可以設定迴圈選項,還可以監控和操作迴圈的狀態。Executes a foreach (For Each in Visual Basic) operation on a OrderablePartitioner<TSource> in which iterations may run in parallel, loop options can be configured, and the state of the loop can be monitored and manipulated.

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

foreach 上執行 For Each (在 Visual Basic 中為 Partitioner) 運算,其中可以平行執行反覆項目,還可以監控和操作迴圈的狀態。Executes a foreach (For Each in Visual Basic) operation on a Partitioner in which iterations may run in parallel, and the state of the loop can be monitored and manipulated.

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

foreach 上執行 For Each (在 Visual Basic 中為 Partitioner) 運算,其中可以平行執行反覆項目。Executes a foreach (For Each in Visual Basic) operation on a Partitioner in which iterations may run in parallel.

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

foreach 上執行 For Each (在 Visual Basic 中為 Partitioner) 運算,其中可以平行執行反覆項目、可以設定迴圈選項,還可以監控和操作迴圈的狀態。Executes a foreach (For Each in Visual Basic) operation on a Partitioner in which iterations may run in parallel, loop options can be configured, and the state of the loop can be monitored and manipulated.

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

foreach 上執行 For Each (在 Visual Basic 中為 Partitioner) 運算,其中可以平行執行反覆項目,還可以設定迴圈選項。Executes a foreach (For Each in Visual Basic) operation on a Partitioner in which iterations may run in parallel and loop options can be configured.

Invoke(Action[])

執行每個提供的動作 (可能會平行執行)。Executes each of the provided actions, possibly in parallel.

Invoke(ParallelOptions, Action[])

除非使用者取消作業,否則執行每個提供的動作 (可能會平行執行)。Executes each of the provided actions, possibly in parallel, unless the operation is cancelled by the user.

適用於

執行緒安全性

Parallel 的所有公開和受保護成員都是安全線程,而且可以從多個執行緒同時使用。All public and protected members of Parallel are thread-safe and may be used concurrently from multiple threads.

另請參閱