Parallel.ForEach 方法

定義

執行 foreach (在 Visual Basic 中為 For Each ) 運算,其中可以平行執行反覆項目。

多載

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

使用執行緒區域資料,在 IEnumerable 上執行 foreach (Visual Basic 中的 For Each) 作業,其中可以平行執行反覆運算、可設定迴圈選項,且可以監視和操作迴圈狀態。

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

使用執行緒區域資料和 64 位元索引在 IEnumerable 上執行 foreach (在 Visual Basic 中為 For Each) 運算,其中可以平行執行反覆項目、可以設定迴圈選項,還可以監控和操作迴圈的狀態。

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

使用執行緒區域資料在 foreach 上執行 For Each (在 Visual Basic 中為 Partitioner) 運算,其中可以平行執行反覆項目、可以設定迴圈選項,還可以監控和操作迴圈的狀態。

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

foreach Visual Basic) 作業中執行具有 64 位索引的 (For Each ,並在 上 OrderablePartitioner<TSource> 執行執行緒本機資料,其中反復專案可以平行執行、迴圈選項可以設定,以及可以監視及操作迴圈的狀態。

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

使用執行緒區域資料在 foreach 上執行 For Each (在 Visual Basic 中為 IEnumerable) 運算,其中可以平行執行反覆項目,還可以監控和操作迴圈的狀態。

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

使用執行緒區域資料在 IEnumerable 上執行 foreach (在 Visual Basic 中為 For Each) 運算,其中可以平行執行反覆項目,還可以監控和操作迴圈的狀態。

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

使用執行緒區域資料在 foreach 上執行 For Each (在 Visual Basic 中為 Partitioner) 運算,其中可以平行執行反覆項目,還可以監控和操作迴圈的狀態。

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

使用執行緒區域資料在 foreach 上執行 For Each (在 Visual Basic 中為 OrderablePartitioner<TSource>) 運算,其中可以平行執行反覆項目、可以設定迴圈選項,還可以監控和操作迴圈的狀態。

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

IEnumerable 上執行 foreach (在 Visual Basic 中為 For Each) 運算,其中可以平行執行反覆項目,還可以設定迴圈選項。

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

IEnumerable 上執行 foreach (在 Visual Basic 中為 For Each) 運算,其中可以平行執行反覆項目、可以設定迴圈選項,還可以監控和操作迴圈的狀態。

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

foreach 上執行 For Each (在 Visual Basic 中為 OrderablePartitioner<TSource>) 運算,其中可以平行執行反覆項目、可以設定迴圈選項,還可以監控和操作迴圈的狀態。

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

Partitioner 上執行 foreach (在 Visual Basic 中為 For Each) 運算,其中可以平行執行反覆項目,還可以設定迴圈選項。

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

使用 64 位元索引在 foreach 上執行 For Each (在 Visual Basic 中為 IEnumerable) 運算,其中可以平行執行反覆項目、可以設定迴圈選項,還可以監控和操作迴圈的狀態。

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

Partitioner 上執行 foreach (在 Visual Basic 中為 For Each) 運算,其中可以平行執行反覆項目,還可以監控和操作迴圈的狀態。

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

foreach 上執行 For Each (在 Visual Basic 中為 Partitioner) 運算,其中可以平行執行反覆項目。

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

使用 64 位元索引在 IEnumerable 上執行 foreach (在 Visual Basic 中為 For Each) 運算,其中可以平行執行反覆項目,還可以監控和操作迴圈的狀態。

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

foreach 上執行 For Each (在 Visual Basic 中為 OrderablePartitioner<TSource>) 運算,其中可以平行執行反覆項目,還可以監控和操作迴圈的狀態。

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

foreach 上執行 For Each (在 Visual Basic 中為 IEnumerable) 運算,其中可以平行執行反覆項目。

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

foreach 上執行 For Each (在 Visual Basic 中為 Partitioner) 運算,其中可以平行執行反覆項目、可以設定迴圈選項,還可以監控和操作迴圈的狀態。

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

IEnumerable 上執行 foreach (在 Visual Basic 中為 For Each) 運算,其中可以平行執行反覆項目,還可以監控和操作迴圈的狀態。

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

Source:
Parallel.cs
Source:
Parallel.cs
Source:
Parallel.cs

使用執行緒區域資料,在 IEnumerable 上執行 foreach (Visual Basic 中的 For Each) 作業,其中可以平行執行反覆運算、可設定迴圈選項,且可以監視和操作迴圈狀態。

public:
generic <typename TSource, typename TLocal>
 static System::Threading::Tasks::ParallelLoopResult ForEach(System::Collections::Generic::IEnumerable<TSource> ^ source, System::Threading::Tasks::ParallelOptions ^ parallelOptions, Func<TLocal> ^ localInit, Func<TSource, System::Threading::Tasks::ParallelLoopState ^, TLocal, TLocal> ^ body, Action<TLocal> ^ localFinally);
public static System.Threading.Tasks.ParallelLoopResult ForEach<TSource,TLocal> (System.Collections.Generic.IEnumerable<TSource> source, System.Threading.Tasks.ParallelOptions parallelOptions, Func<TLocal> localInit, Func<TSource,System.Threading.Tasks.ParallelLoopState,TLocal,TLocal> body, Action<TLocal> localFinally);
static member ForEach : seq<'Source> * System.Threading.Tasks.ParallelOptions * Func<'Local> * Func<'Source, System.Threading.Tasks.ParallelLoopState, 'Local, 'Local> * Action<'Local> -> System.Threading.Tasks.ParallelLoopResult
Public Shared Function ForEach(Of TSource, TLocal) (source As IEnumerable(Of TSource), parallelOptions As ParallelOptions, localInit As Func(Of TLocal), body As Func(Of TSource, ParallelLoopState, TLocal, TLocal), localFinally As Action(Of TLocal)) As ParallelLoopResult

類型參數

TSource

來源中的資料類型。

TLocal

執行緒區域資料的類型。

參數

source
IEnumerable<TSource>

可列舉的資料來源。

parallelOptions
ParallelOptions

物件,設定這個作業的行為。

localInit
Func<TLocal>

函式委派,會針對每項工作傳回本機資料的初始狀態。

body
Func<TSource,ParallelLoopState,TLocal,TLocal>

每個反覆項目都會叫用一次的委派。

localFinally
Action<TLocal>

委派,會針對每項工作的本機狀態執行最終動作。

傳回

包含迴圈已完成部分之相關資訊的的結構。

例外狀況

source 引數為 null

-或-

parallelOptions 引數為 null

-或-

body 引數為 null

-或-

localInit 引數為 null

-或-

localFinally 引數為 null

parallelOptions 引數中的 CancellationToken 已取消。

已處置與 parallelOptions 中的 CancellationTokenSource 相關聯的 CancellationToken

在所有執行緒擲回包含所有個別例外狀況的例外狀況。

備註

委派 body 會針對可列舉中的每個 source 專案叫用一次。 它隨附下列參數:目前專案、 ParallelLoopState 可用來提前中斷迴圈的實例,以及可在相同執行緒上執行的反復專案之間共用的一些本機狀態。

每個 localInit 參與迴圈執行的工作都會叫用委派一次,並傳回每個工作的初始本機狀態。 這些初始狀態會傳遞至每個工作上的第一個 body 調用。 然後,每個後續的主體調用都會傳回可能修改的狀態值,這個值會傳遞至下一個主體調用。 最後,每個工作的最後一個主體調用會傳回傳遞至 localFinally 委派的狀態值。 每個 localFinally 執行緒都會叫用委派一次,以在每個工作的本機狀態上執行最終動作。 此委派可能會在多個工作上同時叫用;因此,您必須同步存取任何共用變數。

方法 Parallel.ForEach 在執行期間可能會使用比執行緒更多的工作,因為現有的工作已完成,而且會由新工作取代。 這可讓基礎 TaskScheduler 物件有機會新增、變更或移除服務迴圈的執行緒。

另請參閱

適用於

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

Source:
Parallel.cs
Source:
Parallel.cs
Source:
Parallel.cs

使用執行緒區域資料和 64 位元索引在 IEnumerable 上執行 foreach (在 Visual Basic 中為 For Each) 運算,其中可以平行執行反覆項目、可以設定迴圈選項,還可以監控和操作迴圈的狀態。

public:
generic <typename TSource, typename TLocal>
 static System::Threading::Tasks::ParallelLoopResult ForEach(System::Collections::Generic::IEnumerable<TSource> ^ source, System::Threading::Tasks::ParallelOptions ^ parallelOptions, Func<TLocal> ^ localInit, Func<TSource, System::Threading::Tasks::ParallelLoopState ^, long, TLocal, TLocal> ^ body, Action<TLocal> ^ localFinally);
public static System.Threading.Tasks.ParallelLoopResult ForEach<TSource,TLocal> (System.Collections.Generic.IEnumerable<TSource> source, System.Threading.Tasks.ParallelOptions parallelOptions, Func<TLocal> localInit, Func<TSource,System.Threading.Tasks.ParallelLoopState,long,TLocal,TLocal> body, Action<TLocal> localFinally);
static member ForEach : seq<'Source> * System.Threading.Tasks.ParallelOptions * Func<'Local> * Func<'Source, System.Threading.Tasks.ParallelLoopState, int64, 'Local, 'Local> * Action<'Local> -> System.Threading.Tasks.ParallelLoopResult
Public Shared Function ForEach(Of TSource, TLocal) (source As IEnumerable(Of TSource), parallelOptions As ParallelOptions, localInit As Func(Of TLocal), body As Func(Of TSource, ParallelLoopState, Long, TLocal, TLocal), localFinally As Action(Of TLocal)) As ParallelLoopResult

類型參數

TSource

來源中的資料類型。

TLocal

執行緒區域資料的類型。

參數

source
IEnumerable<TSource>

可列舉的資料來源。

parallelOptions
ParallelOptions

物件,設定這個作業的行為。

localInit
Func<TLocal>

函式委派,會針對每項工作傳回本機資料的初始狀態。

body
Func<TSource,ParallelLoopState,Int64,TLocal,TLocal>

每個反覆項目都會叫用一次的委派。

localFinally
Action<TLocal>

委派,會針對每項工作的本機狀態執行最終動作。

傳回

包含迴圈已完成部分之相關資訊的的結構。

例外狀況

source 引數為 null

-或-

parallelOptions 引數為 null

-或-

body 引數為 null

-或-

localInit 引數為 null

-或-

localFinally 引數為 null

parallelOptions 引數中的 CancellationToken 已取消。

已處置與 parallelOptions 中的 CancellationTokenSource 相關聯的 CancellationToken

在所有執行緒擲回包含所有個別例外狀況的例外狀況。

備註

委派 body 會針對可列舉中的每個 source 專案叫用一次。 它隨附下列參數:目前專案、 ParallelLoopState 可用來提前中斷迴圈的實例、目前元素的索引 () Int64 ,以及一些可能會在相同執行緒上執行的反復專案之間共用的本機狀態。

每個 localInit 參與迴圈執行的工作都會叫用委派一次,並傳回每個工作的初始本機狀態。 這些初始狀態會傳遞至每個工作上的第一個 body 調用。 然後,每個後續的主體調用都會傳回可能修改的狀態值,這個值會傳遞至下一個主體調用。 最後,每個工作的最後一個主體調用會傳回傳遞至 localFinally 委派的狀態值。 每個 localFinally 執行緒都會叫用委派一次,以在每個工作的本機狀態上執行最終動作。 此委派可能會在多個工作上同時叫用;因此,您必須同步存取任何共用變數。

方法 Parallel.ForEach 在執行期間可能會使用比執行緒更多的工作,因為現有的工作已完成,而且會由新工作取代。 這可讓基礎 TaskScheduler 物件有機會新增、變更或移除服務迴圈的執行緒。

另請參閱

適用於

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

Source:
Parallel.cs
Source:
Parallel.cs
Source:
Parallel.cs

使用執行緒區域資料在 foreach 上執行 For Each (在 Visual Basic 中為 Partitioner) 運算,其中可以平行執行反覆項目、可以設定迴圈選項,還可以監控和操作迴圈的狀態。

public:
generic <typename TSource, typename TLocal>
 static System::Threading::Tasks::ParallelLoopResult ForEach(System::Collections::Concurrent::Partitioner<TSource> ^ source, System::Threading::Tasks::ParallelOptions ^ parallelOptions, Func<TLocal> ^ localInit, Func<TSource, System::Threading::Tasks::ParallelLoopState ^, TLocal, TLocal> ^ body, Action<TLocal> ^ localFinally);
public static System.Threading.Tasks.ParallelLoopResult ForEach<TSource,TLocal> (System.Collections.Concurrent.Partitioner<TSource> source, System.Threading.Tasks.ParallelOptions parallelOptions, Func<TLocal> localInit, Func<TSource,System.Threading.Tasks.ParallelLoopState,TLocal,TLocal> body, Action<TLocal> localFinally);
static member ForEach : System.Collections.Concurrent.Partitioner<'Source> * System.Threading.Tasks.ParallelOptions * Func<'Local> * Func<'Source, System.Threading.Tasks.ParallelLoopState, 'Local, 'Local> * Action<'Local> -> System.Threading.Tasks.ParallelLoopResult
Public Shared Function ForEach(Of TSource, TLocal) (source As Partitioner(Of TSource), parallelOptions As ParallelOptions, localInit As Func(Of TLocal), body As Func(Of TSource, ParallelLoopState, TLocal, TLocal), localFinally As Action(Of TLocal)) As ParallelLoopResult

類型參數

TSource

source 中的項目類型。

TLocal

執行緒區域資料的類型。

參數

source
Partitioner<TSource>

包含原始資料來源的 Partitioner。

parallelOptions
ParallelOptions

物件,設定這個作業的行為。

localInit
Func<TLocal>

函式委派,會針對每項工作傳回本機資料的初始狀態。

body
Func<TSource,ParallelLoopState,TLocal,TLocal>

每個反覆項目都會叫用一次的委派。

localFinally
Action<TLocal>

委派,會針對每項工作的本機狀態執行最終動作。

傳回

包含迴圈已完成部分之相關資訊的的結構。

例外狀況

source 引數為 null

-或-

parallelOptions 引數為 null

-或-

body 引數為 null

-或-

localInit 引數為 null

-或-

localFinally 引數為 null

sourcePartitioner 中的 SupportsDynamicPartitions 屬性會傳回 false,或者 Partitioner 會傳回 null 分割。

在所有執行緒擲回包含所有個別例外狀況的例外狀況。

parallelOptions 引數中的 CancellationToken 已取消。

已處置與 parallelOptions 中的 CancellationTokenSource 相關聯的 CancellationToken

備註

此多載適用于您想要覆寫預設資料分割配置的案例。 例如,小型迴圈主體可能會受益于分割範圍。 方法 ForEach 預期自訂資料分割器支援動態資料分割。 此多載適用于可能會受益于靜態範圍分割的小型迴圈主體案例。 資料分割器必須支援動態資料分割。 如需詳細資訊,請參閱PLINQ 和 TPL 的自訂資料分割器和如何:實作動態資料分割

每個 localInit 參與迴圈執行的工作都會叫用委派一次,並傳回每個工作的初始本機狀態。 這些初始狀態會傳遞至每個工作上的第一個 body 調用。 然後,每個後續的主體調用都會傳回可能修改的狀態值,這個值會傳遞至下一個主體調用。 最後,每個工作的最後一個主體調用會傳回傳遞至 localFinally 委派的狀態值。 每個 localFinally 工作都會叫用委派一次,以在每個工作的本機狀態上執行最終動作。 此委派可能會在多個工作上同時叫用;因此,您必須同步存取任何共用變數。

方法 Parallel.ForEach 在執行期間可能會使用比執行緒更多的工作,因為現有的工作已完成,而且會由新工作取代。 這可讓基礎 TaskScheduler 物件有機會新增、變更或移除服務迴圈的執行緒。

另請參閱

適用於

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

Source:
Parallel.cs
Source:
Parallel.cs
Source:
Parallel.cs

foreach Visual Basic) 作業中執行具有 64 位索引的 (For Each ,並在 上 OrderablePartitioner<TSource> 執行執行緒本機資料,其中反復專案可以平行執行、迴圈選項可以設定,以及可以監視及操作迴圈的狀態。

public:
generic <typename TSource, typename TLocal>
 static System::Threading::Tasks::ParallelLoopResult ForEach(System::Collections::Concurrent::OrderablePartitioner<TSource> ^ source, System::Threading::Tasks::ParallelOptions ^ parallelOptions, Func<TLocal> ^ localInit, Func<TSource, System::Threading::Tasks::ParallelLoopState ^, long, TLocal, TLocal> ^ body, Action<TLocal> ^ localFinally);
public static System.Threading.Tasks.ParallelLoopResult ForEach<TSource,TLocal> (System.Collections.Concurrent.OrderablePartitioner<TSource> source, System.Threading.Tasks.ParallelOptions parallelOptions, Func<TLocal> localInit, Func<TSource,System.Threading.Tasks.ParallelLoopState,long,TLocal,TLocal> body, Action<TLocal> localFinally);
static member ForEach : System.Collections.Concurrent.OrderablePartitioner<'Source> * System.Threading.Tasks.ParallelOptions * Func<'Local> * Func<'Source, System.Threading.Tasks.ParallelLoopState, int64, 'Local, 'Local> * Action<'Local> -> System.Threading.Tasks.ParallelLoopResult
Public Shared Function ForEach(Of TSource, TLocal) (source As OrderablePartitioner(Of TSource), parallelOptions As ParallelOptions, localInit As Func(Of TLocal), body As Func(Of TSource, ParallelLoopState, Long, TLocal, TLocal), localFinally As Action(Of TLocal)) As ParallelLoopResult

類型參數

TSource

source 中的項目類型。

TLocal

執行緒區域資料的類型。

參數

source
OrderablePartitioner<TSource>

包含原始資料來源的可排序 Partitioner。

parallelOptions
ParallelOptions

物件,設定這個作業的行為。

localInit
Func<TLocal>

函式委派,會針對每項工作傳回本機資料的初始狀態。

body
Func<TSource,ParallelLoopState,Int64,TLocal,TLocal>

每個反覆項目都會叫用一次的委派。

localFinally
Action<TLocal>

委派,會針對每項工作的本機狀態執行最終動作。

傳回

包含迴圈已完成部分之相關資訊的的結構。

例外狀況

source 引數為 null

-或-

parallelOptions 引數為 null

-或-

body 引數為 null

-或-

localInitlocalFinally 引數為 null

sourcePartitioner 中的 SupportsDynamicPartitions 屬性會傳回 false,或者 Partitioner 會傳回 null 分割。

在所有執行緒擲回包含所有個別例外狀況的例外狀況。

parallelOptions 引數中的 CancellationToken 已取消。

已處置與 parallelOptions 中的 CancellationTokenSource 相關聯的 CancellationToken

備註

此多載適用于您想要覆寫預設資料分割配置的案例。 例如,小型迴圈主體可能會受益于分割範圍。 方法 ForEach 預期自訂資料分割器支援動態資料分割。 如需詳細資訊,請參閱PLINQ 和 TPL 的自訂資料分割器和如何:實作動態資料分割

每個 localInit 參與迴圈執行的工作都會叫用委派一次,並傳回每個工作的初始本機狀態。 這些初始狀態會傳遞至每個工作上的第一個 body 調用。 然後,每個後續的主體調用都會傳回可能修改的狀態值,這個值會傳遞至下一個主體調用。 最後,每個執行緒上的最後一個主體調用會傳回傳遞至 localFinally 委派的狀態值。 每個 localFinally 工作都會叫用委派一次,以在每個工作的本機狀態上執行最終動作。 此委派可能會在多個工作上同時叫用;因此,您必須同步存取任何共用變數。

方法 Parallel.ForEach 在執行期間可能會使用比執行緒更多的工作,因為現有的工作已完成,而且會由新工作取代。 這可讓基礎 TaskScheduler 物件有機會新增、變更或移除服務迴圈的執行緒。

另請參閱

適用於

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

Source:
Parallel.cs
Source:
Parallel.cs
Source:
Parallel.cs

使用執行緒區域資料在 foreach 上執行 For Each (在 Visual Basic 中為 IEnumerable) 運算,其中可以平行執行反覆項目,還可以監控和操作迴圈的狀態。

public:
generic <typename TSource, typename TLocal>
 static System::Threading::Tasks::ParallelLoopResult ForEach(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TLocal> ^ localInit, Func<TSource, System::Threading::Tasks::ParallelLoopState ^, TLocal, TLocal> ^ body, Action<TLocal> ^ localFinally);
public static System.Threading.Tasks.ParallelLoopResult ForEach<TSource,TLocal> (System.Collections.Generic.IEnumerable<TSource> source, Func<TLocal> localInit, Func<TSource,System.Threading.Tasks.ParallelLoopState,TLocal,TLocal> body, Action<TLocal> localFinally);
static member ForEach : seq<'Source> * Func<'Local> * Func<'Source, System.Threading.Tasks.ParallelLoopState, 'Local, 'Local> * Action<'Local> -> System.Threading.Tasks.ParallelLoopResult
Public Shared Function ForEach(Of TSource, TLocal) (source As IEnumerable(Of TSource), localInit As Func(Of TLocal), body As Func(Of TSource, ParallelLoopState, TLocal, TLocal), localFinally As Action(Of TLocal)) As ParallelLoopResult

類型參數

TSource

來源中的資料類型。

TLocal

執行緒區域資料的類型。

參數

source
IEnumerable<TSource>

可列舉的資料來源。

localInit
Func<TLocal>

函式委派,會針對每項工作傳回本機資料的初始狀態。

body
Func<TSource,ParallelLoopState,TLocal,TLocal>

每個反覆項目都會叫用一次的委派。

localFinally
Action<TLocal>

委派,會針對每項工作的本機狀態執行最終動作。

傳回

包含迴圈已完成部分之相關資訊的的結構。

例外狀況

source 引數為 null

-或-

body 引數為 null

-或-

localInit 引數為 null

-或-

localFinally 引數為 null

在所有執行緒擲回包含所有個別例外狀況的例外狀況。

範例

下列範例示範如何使用具有 ForEach 本機狀態的方法:

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

class ForEachWithThreadLocal
{
    // Demonstrated features:
    // 		Parallel.ForEach()
    //		Thread-local state
    // Expected results:
    //      This example sums up the elements of an int[] in parallel.
    //      Each thread maintains a local sum. When a thread is initialized, that local sum is set to 0.
    //      On every iteration the current element is added to the local sum.
    //      When a thread is done, it safely adds its local sum to the global sum.
    //      After the loop is complete, the global sum is printed out.
    // Documentation:
    //		http://msdn.microsoft.com/library/dd990270(VS.100).aspx
    static void Main()
    {
        // The sum of these elements is 40.
        int[] input = { 4, 1, 6, 2, 9, 5, 10, 3 };
        int sum = 0;

        try
        {
            Parallel.ForEach(
                    input,					        // source collection
                    () => 0,					        // thread local initializer
                    (n, loopState, localSum) =>		// body
                    {
                        localSum += n;
                        Console.WriteLine("Thread={0}, n={1}, localSum={2}", Thread.CurrentThread.ManagedThreadId, n, localSum);
                        return localSum;
                    },
                    (localSum) => Interlocked.Add(ref sum, localSum)					// thread local aggregator
                );

            Console.WriteLine("\nSum={0}", sum);
        }
        // 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("Parallel.ForEach has thrown an exception. THIS WAS NOT EXPECTED.\n{0}", e);
        }
    }
}
Imports System.Threading
Imports System.Threading.Tasks

Module ForEachDemo

    ' Demonstrated features:
    '   Parallel.ForEach()
    '   Thread-local state
    ' Expected results:
    '   This example sums up the elements of an int[] in parallel.
    '   Each thread maintains a local sum. When a thread is initialized, that local sum is set to 0.
    '   On every iteration the current element is added to the local sum.
    '   When a thread is done, it safely adds its local sum to the global sum.
    '   After the loop is complete, the global sum is printed out.
    ' Documentation:
    '   http://msdn.microsoft.com/library/dd990270(VS.100).aspx
    Private Sub ForEachDemo()
        ' The sum of these elements is 40.
        Dim input As Integer() = {4, 1, 6, 2, 9, 5, _
        10, 3}
        Dim sum As Integer = 0

        Try
            ' source collection
            Parallel.ForEach(input,
                             Function()
                                 ' thread local initializer
                                 Return 0
                             End Function,
                             Function(n, loopState, localSum)
                                 ' body
                                 localSum += n
                                 Console.WriteLine("Thread={0}, n={1}, localSum={2}", Thread.CurrentThread.ManagedThreadId, n, localSum)
                                 Return localSum
                             End Function,
                             Sub(localSum)
                                 ' thread local aggregator
                                 Interlocked.Add(sum, localSum)
                             End Sub)

            Console.WriteLine(vbLf & "Sum={0}", sum)
        Catch e As AggregateException
            ' 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.
            Console.WriteLine("Parallel.ForEach has thrown an exception. THIS WAS NOT EXPECTED." & vbLf & "{0}", e)
        End Try
    End Sub


End Module

備註

委派 body 會針對可列舉中的每個 source 專案叫用一次。 它隨附下列參數:目前專案、 ParallelLoopState 可用來提前中斷迴圈的實例,以及可在相同執行緒上執行的反復專案之間共用的一些本機狀態。

每個 localInit 參與迴圈執行的工作都會叫用委派一次,並傳回每個工作的初始本機狀態。 這些初始狀態會傳遞至每個工作上的第一個 body 調用。 然後,每個後續的主體調用都會傳回可能修改的狀態值,這個值會傳遞至下一個主體調用。 最後,每個工作的最後一個主體調用會傳回傳遞至 localFinally 委派的狀態值。 每個 localFinally 執行緒都會叫用委派一次,以在每個工作的本機狀態上執行最終動作。 此委派可能會在多個工作上同時叫用;因此,您必須同步存取任何共用變數。

方法 Parallel.ForEach 在執行期間可能會使用比執行緒更多的工作,因為現有的工作已完成,而且會由新工作取代。 這可讓基礎 TaskScheduler 物件有機會新增、變更或移除服務迴圈的執行緒。

另請參閱

適用於

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

Source:
Parallel.cs
Source:
Parallel.cs
Source:
Parallel.cs

使用執行緒區域資料在 IEnumerable 上執行 foreach (在 Visual Basic 中為 For Each) 運算,其中可以平行執行反覆項目,還可以監控和操作迴圈的狀態。

public:
generic <typename TSource, typename TLocal>
 static System::Threading::Tasks::ParallelLoopResult ForEach(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TLocal> ^ localInit, Func<TSource, System::Threading::Tasks::ParallelLoopState ^, long, TLocal, TLocal> ^ body, Action<TLocal> ^ localFinally);
public static System.Threading.Tasks.ParallelLoopResult ForEach<TSource,TLocal> (System.Collections.Generic.IEnumerable<TSource> source, Func<TLocal> localInit, Func<TSource,System.Threading.Tasks.ParallelLoopState,long,TLocal,TLocal> body, Action<TLocal> localFinally);
static member ForEach : seq<'Source> * Func<'Local> * Func<'Source, System.Threading.Tasks.ParallelLoopState, int64, 'Local, 'Local> * Action<'Local> -> System.Threading.Tasks.ParallelLoopResult
Public Shared Function ForEach(Of TSource, TLocal) (source As IEnumerable(Of TSource), localInit As Func(Of TLocal), body As Func(Of TSource, ParallelLoopState, Long, TLocal, TLocal), localFinally As Action(Of TLocal)) As ParallelLoopResult

類型參數

TSource

來源中的資料類型。

TLocal

執行緒區域資料的類型。

參數

source
IEnumerable<TSource>

可列舉的資料來源。

localInit
Func<TLocal>

函式委派,會針對每項工作傳回本機資料的初始狀態。

body
Func<TSource,ParallelLoopState,Int64,TLocal,TLocal>

每個反覆項目都會叫用一次的委派。

localFinally
Action<TLocal>

委派,會針對每項工作的本機狀態執行最終動作。

傳回

包含迴圈已完成部分之相關資訊的的結構。

例外狀況

source 引數為 null

-或-

body 引數為 null

-或-

localInit 引數為 null

-或-

localFinally 引數為 null

在所有執行緒擲回包含所有個別例外狀況的例外狀況。

備註

委派 body 會針對可列舉中的每個 source 專案叫用一次。 它隨附下列參數:目前專案、 ParallelLoopState 可用來提前中斷迴圈的實例、目前元素的索引 () Int64 ,以及一些可能會在相同執行緒上執行的反復專案之間共用的本機狀態。

每個 localInit 參與迴圈執行的工作都會叫用委派一次,並傳回每個工作的初始本機狀態。 這些初始狀態會傳遞至每個工作上的第一個 body 調用。 然後,每個後續的主體調用都會傳回可能修改的狀態值,這個值會傳遞至下一個主體調用。 最後,每個工作的最後一個主體調用會傳回傳遞至 localFinally 委派的狀態值。 每個 localFinally 工作都會叫用委派一次,以在每個工作的本機狀態上執行最終動作。 此委派可能會在多個工作上同時叫用;因此,您必須同步存取任何共用變數。

方法 Parallel.ForEach 在執行期間可能會使用比執行緒更多的工作,因為現有的工作已完成,而且會由新工作取代。 這可讓基礎 TaskScheduler 物件有機會新增、變更或移除服務迴圈的執行緒。

另請參閱

適用於

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

Source:
Parallel.cs
Source:
Parallel.cs
Source:
Parallel.cs

使用執行緒區域資料在 foreach 上執行 For Each (在 Visual Basic 中為 Partitioner) 運算,其中可以平行執行反覆項目,還可以監控和操作迴圈的狀態。

public:
generic <typename TSource, typename TLocal>
 static System::Threading::Tasks::ParallelLoopResult ForEach(System::Collections::Concurrent::Partitioner<TSource> ^ source, Func<TLocal> ^ localInit, Func<TSource, System::Threading::Tasks::ParallelLoopState ^, TLocal, TLocal> ^ body, Action<TLocal> ^ localFinally);
public static System.Threading.Tasks.ParallelLoopResult ForEach<TSource,TLocal> (System.Collections.Concurrent.Partitioner<TSource> source, Func<TLocal> localInit, Func<TSource,System.Threading.Tasks.ParallelLoopState,TLocal,TLocal> body, Action<TLocal> localFinally);
static member ForEach : System.Collections.Concurrent.Partitioner<'Source> * Func<'Local> * Func<'Source, System.Threading.Tasks.ParallelLoopState, 'Local, 'Local> * Action<'Local> -> System.Threading.Tasks.ParallelLoopResult
Public Shared Function ForEach(Of TSource, TLocal) (source As Partitioner(Of TSource), localInit As Func(Of TLocal), body As Func(Of TSource, ParallelLoopState, TLocal, TLocal), localFinally As Action(Of TLocal)) As ParallelLoopResult

類型參數

TSource

source 中的項目類型。

TLocal

執行緒區域資料的類型。

參數

source
Partitioner<TSource>

包含原始資料來源的 Partitioner。

localInit
Func<TLocal>

函式委派,會針對每項工作傳回本機資料的初始狀態。

body
Func<TSource,ParallelLoopState,TLocal,TLocal>

每個反覆項目都會叫用一次的委派。

localFinally
Action<TLocal>

委派,會針對每項工作的本機狀態執行最終動作。

傳回

包含迴圈已完成部分之相關資訊的的結構。

例外狀況

source 引數為 null

-或-

body 引數為 null

-或-

localInit 引數為 null

-或-

localFinally 引數為 null

sourcePartitioner 中的 SupportsDynamicPartitions 屬性會傳回 false,或者 Partitioner 會傳回 null 分割。

在所有執行緒擲回包含所有個別例外狀況的例外狀況。

備註

此多載適用于您想要覆寫預設資料分割配置的案例。 例如,小型迴圈主體可能會受益于分割範圍。 方法 ForEach 預期自訂資料分割器支援動態資料分割。 如需詳細資訊,請參閱PLINQ 和 TPL 的自訂資料分割器和如何:實作動態資料分割

每個 localInit 參與迴圈執行的執行緒都會叫用委派一次,並針對每個工作傳回初始本機狀態。 這些初始狀態會傳遞至每個工作上的第一個 body 調用。 然後,每個後續的主體調用都會傳回可能修改的狀態值,這個值會傳遞至下一個主體調用。 最後,每個工作的最後一個主體調用會傳回傳遞至 localFinally 委派的狀態值。 每個 localFinally 工作都會叫用委派一次,以在每個工作的本機狀態上執行最終動作。 此委派可能會在多個工作上同時叫用;因此,您必須同步存取任何共用變數。

方法 Parallel.ForEach 在執行期間可能會使用比執行緒更多的工作,因為現有的工作已完成,而且會由新工作取代。 這可讓基礎 TaskScheduler 物件有機會新增、變更或移除服務迴圈的執行緒。

另請參閱

適用於

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

Source:
Parallel.cs
Source:
Parallel.cs
Source:
Parallel.cs

使用執行緒區域資料在 foreach 上執行 For Each (在 Visual Basic 中為 OrderablePartitioner<TSource>) 運算,其中可以平行執行反覆項目、可以設定迴圈選項,還可以監控和操作迴圈的狀態。

public:
generic <typename TSource, typename TLocal>
 static System::Threading::Tasks::ParallelLoopResult ForEach(System::Collections::Concurrent::OrderablePartitioner<TSource> ^ source, Func<TLocal> ^ localInit, Func<TSource, System::Threading::Tasks::ParallelLoopState ^, long, TLocal, TLocal> ^ body, Action<TLocal> ^ localFinally);
public static System.Threading.Tasks.ParallelLoopResult ForEach<TSource,TLocal> (System.Collections.Concurrent.OrderablePartitioner<TSource> source, Func<TLocal> localInit, Func<TSource,System.Threading.Tasks.ParallelLoopState,long,TLocal,TLocal> body, Action<TLocal> localFinally);
static member ForEach : System.Collections.Concurrent.OrderablePartitioner<'Source> * Func<'Local> * Func<'Source, System.Threading.Tasks.ParallelLoopState, int64, 'Local, 'Local> * Action<'Local> -> System.Threading.Tasks.ParallelLoopResult
Public Shared Function ForEach(Of TSource, TLocal) (source As OrderablePartitioner(Of TSource), localInit As Func(Of TLocal), body As Func(Of TSource, ParallelLoopState, Long, TLocal, TLocal), localFinally As Action(Of TLocal)) As ParallelLoopResult

類型參數

TSource

source 中的項目類型。

TLocal

執行緒區域資料的類型。

參數

source
OrderablePartitioner<TSource>

包含原始資料來源的可排序 Partitioner。

localInit
Func<TLocal>

函式委派,會針對每項工作傳回本機資料的初始狀態。

body
Func<TSource,ParallelLoopState,Int64,TLocal,TLocal>

每個反覆項目都會叫用一次的委派。

localFinally
Action<TLocal>

委派,會針對每項工作的本機狀態執行最終動作。

傳回

包含迴圈已完成部分之相關資訊的的結構。

例外狀況

source 引數為 null

-或-

body 引數為 null

-或-

localInit 引數為 null

-或-

localFinally 引數為 null

sourcePartitioner 中的 SupportsDynamicPartitions 屬性會傳回 false,或者 Partitioner 會傳回 null 分割。

在所有執行緒擲回包含所有個別例外狀況的例外狀況。

備註

此多載適用于您想要覆寫預設資料分割配置的案例。 例如,小型迴圈主體可能會受益于分割範圍。 方法 ForEach 預期自訂資料分割器支援動態資料分割。 如需詳細資訊,請參閱PLINQ 和 TPL 的自訂資料分割器和如何:實作動態資料分割

每個 localInit 參與迴圈執行的工作都會叫用委派一次,並傳回每個工作的初始本機狀態。 這些初始狀態會傳遞至每個工作上的第一個 body 調用。 然後,每個後續的主體調用都會傳回可能修改的狀態值,這個值會傳遞至下一個主體調用。 最後,每個工作的最後一個主體調用會傳回傳遞至 localFinally 委派的狀態值。 每個 localFinally 工作都會叫用委派一次,以在每個工作的本機狀態上執行最終動作。 此委派可能會在多個工作上同時叫用;因此,您必須同步存取任何共用變數。

方法 Parallel.ForEach 在執行期間可能會使用比執行緒更多的工作,因為現有的工作已完成,而且會由新工作取代。 這可讓基礎 TaskScheduler 物件有機會新增、變更或移除服務迴圈的執行緒。

另請參閱

適用於

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

Source:
Parallel.cs
Source:
Parallel.cs
Source:
Parallel.cs

IEnumerable 上執行 foreach (在 Visual Basic 中為 For Each) 運算,其中可以平行執行反覆項目,還可以設定迴圈選項。

public:
generic <typename TSource>
 static System::Threading::Tasks::ParallelLoopResult ForEach(System::Collections::Generic::IEnumerable<TSource> ^ source, System::Threading::Tasks::ParallelOptions ^ parallelOptions, Action<TSource> ^ body);
public static System.Threading.Tasks.ParallelLoopResult ForEach<TSource> (System.Collections.Generic.IEnumerable<TSource> source, System.Threading.Tasks.ParallelOptions parallelOptions, Action<TSource> body);
static member ForEach : seq<'Source> * System.Threading.Tasks.ParallelOptions * Action<'Source> -> System.Threading.Tasks.ParallelLoopResult
Public Shared Function ForEach(Of TSource) (source As IEnumerable(Of TSource), parallelOptions As ParallelOptions, body As Action(Of TSource)) As ParallelLoopResult

類型參數

TSource

來源中的資料類型。

參數

source
IEnumerable<TSource>

可列舉的資料來源。

parallelOptions
ParallelOptions

物件,設定這個作業的行為。

body
Action<TSource>

每個反覆項目都會叫用一次的委派。

傳回

包含迴圈已完成部分之相關資訊的的結構。

例外狀況

parallelOptions 引數中的 CancellationToken 已取消

source 引數為 null

-或-

parallelOptions 引數為 null

-或-

body 引數為 null

在所有執行緒擲回包含所有個別例外狀況的例外狀況。

已處置與 parallelOptions 中的 CancellationTokenSource 相關聯的 CancellationToken

備註

委派 body 會針對可列舉中的每個 source 專案叫用一次。 它會以目前專案做為參數提供。

另請參閱

適用於

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

Source:
Parallel.cs
Source:
Parallel.cs
Source:
Parallel.cs

IEnumerable 上執行 foreach (在 Visual Basic 中為 For Each) 運算,其中可以平行執行反覆項目、可以設定迴圈選項,還可以監控和操作迴圈的狀態。

public:
generic <typename TSource>
 static System::Threading::Tasks::ParallelLoopResult ForEach(System::Collections::Generic::IEnumerable<TSource> ^ source, System::Threading::Tasks::ParallelOptions ^ parallelOptions, Action<TSource, System::Threading::Tasks::ParallelLoopState ^> ^ body);
public static System.Threading.Tasks.ParallelLoopResult ForEach<TSource> (System.Collections.Generic.IEnumerable<TSource> source, System.Threading.Tasks.ParallelOptions parallelOptions, Action<TSource,System.Threading.Tasks.ParallelLoopState> body);
static member ForEach : seq<'Source> * System.Threading.Tasks.ParallelOptions * Action<'Source, System.Threading.Tasks.ParallelLoopState> -> System.Threading.Tasks.ParallelLoopResult
Public Shared Function ForEach(Of TSource) (source As IEnumerable(Of TSource), parallelOptions As ParallelOptions, body As Action(Of TSource, ParallelLoopState)) As ParallelLoopResult

類型參數

TSource

來源中的資料類型。

參數

source
IEnumerable<TSource>

可列舉的資料來源。

parallelOptions
ParallelOptions

物件,設定這個作業的行為。

body
Action<TSource,ParallelLoopState>

每個反覆項目都會叫用一次的委派。

傳回

包含迴圈已完成部分之相關資訊的的結構。

例外狀況

parallelOptions 引數中的 CancellationToken 已取消

source 引數為 null

-或-

parallelOptions 引數為 null

-或-

body 引數為 null

在所有執行緒擲回包含所有個別例外狀況的例外狀況。

已處置與 parallelOptions 中的 CancellationTokenSource 相關聯的 CancellationToken

備註

委派 body 會針對可列舉中的每個 source 專案叫用一次。 它隨附下列參數:目前的 元素,以及 ParallelLoopState 可用來提前中斷迴圈的 實例。

另請參閱

適用於

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

Source:
Parallel.cs
Source:
Parallel.cs
Source:
Parallel.cs

foreach 上執行 For Each (在 Visual Basic 中為 OrderablePartitioner<TSource>) 運算,其中可以平行執行反覆項目、可以設定迴圈選項,還可以監控和操作迴圈的狀態。

public:
generic <typename TSource>
 static System::Threading::Tasks::ParallelLoopResult ForEach(System::Collections::Concurrent::OrderablePartitioner<TSource> ^ source, System::Threading::Tasks::ParallelOptions ^ parallelOptions, Action<TSource, System::Threading::Tasks::ParallelLoopState ^, long> ^ body);
public static System.Threading.Tasks.ParallelLoopResult ForEach<TSource> (System.Collections.Concurrent.OrderablePartitioner<TSource> source, System.Threading.Tasks.ParallelOptions parallelOptions, Action<TSource,System.Threading.Tasks.ParallelLoopState,long> body);
static member ForEach : System.Collections.Concurrent.OrderablePartitioner<'Source> * System.Threading.Tasks.ParallelOptions * Action<'Source, System.Threading.Tasks.ParallelLoopState, int64> -> System.Threading.Tasks.ParallelLoopResult
Public Shared Function ForEach(Of TSource) (source As OrderablePartitioner(Of TSource), parallelOptions As ParallelOptions, body As Action(Of TSource, ParallelLoopState, Long)) As ParallelLoopResult

類型參數

TSource

source 中的項目類型。

參數

source
OrderablePartitioner<TSource>

包含原始資料來源的可排序 Partitioner。

parallelOptions
ParallelOptions

物件,設定這個作業的行為。

body
Action<TSource,ParallelLoopState,Int64>

每個反覆項目都會叫用一次的委派。

傳回

包含迴圈已完成部分之相關資訊的的結構。

例外狀況

parallelOptions 引數中的 CancellationToken 已取消

source 引數為 null

-或-

parallelOptions 引數為 null

-或-

body 引數為 null

已處置與 parallelOptions 中的 CancellationTokenSource 相關聯的 CancellationToken

source 可排序 Partitioner 中的 SupportsDynamicPartitions 屬性會傳回 false

-或-

source 可排序 Partitioner 中的 KeysNormalized 屬性會傳回 false

-或-

source 可排列 Partitioner 中的任何方法傳回 null 時,擲回的例外狀況。

擲回的例外狀況包含從其中一個指定委派擲回的例外狀況。

備註

針對您想要覆寫預設資料分割配置的案例,會提供此多載。 例如,小型迴圈主體可能會受益于分割範圍。 方法 Parallel.ForEach 預期自訂資料分割器支援動態資料分割。 如需詳細資訊,請參閱 PLINQ 和 TPL 的自訂資料分割器如何:實作動態資料分割

另請參閱

適用於

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

Source:
Parallel.cs
Source:
Parallel.cs
Source:
Parallel.cs

Partitioner 上執行 foreach (在 Visual Basic 中為 For Each) 運算,其中可以平行執行反覆項目,還可以設定迴圈選項。

public:
generic <typename TSource>
 static System::Threading::Tasks::ParallelLoopResult ForEach(System::Collections::Concurrent::Partitioner<TSource> ^ source, System::Threading::Tasks::ParallelOptions ^ parallelOptions, Action<TSource> ^ body);
public static System.Threading.Tasks.ParallelLoopResult ForEach<TSource> (System.Collections.Concurrent.Partitioner<TSource> source, System.Threading.Tasks.ParallelOptions parallelOptions, Action<TSource> body);
static member ForEach : System.Collections.Concurrent.Partitioner<'Source> * System.Threading.Tasks.ParallelOptions * Action<'Source> -> System.Threading.Tasks.ParallelLoopResult
Public Shared Function ForEach(Of TSource) (source As Partitioner(Of TSource), parallelOptions As ParallelOptions, body As Action(Of TSource)) As ParallelLoopResult

類型參數

TSource

source 中的項目類型。

參數

source
Partitioner<TSource>

包含原始資料來源的 Partitioner。

parallelOptions
ParallelOptions

物件,設定這個作業的行為。

body
Action<TSource>

每個反覆項目都會叫用一次的委派。

傳回

包含迴圈已完成部分之相關資訊的的結構。

例外狀況

parallelOptions 引數中的 CancellationToken 已取消。

已處置與 parallelOptions 中的 CancellationTokenSource 相關聯的 CancellationToken

source 引數為 null

-或-

parallelOptions 引數為 null

-或-

body 引數為 null

source Partitioner 中的 SupportsDynamicPartitions 屬性會傳回 false

-或-

source Partitioner 中的任何方法傳回 null 時,擲回的例外狀況。

擲回的例外狀況包含從其中一個指定委派擲回的例外狀況。

備註

針對您想要覆寫預設資料分割配置的案例,會提供此多載。 例如,小型迴圈主體可能會受益于分割範圍。 方法 Parallel.ForEach 預期自訂資料分割器支援動態資料分割。 如需詳細資訊,請參閱 PLINQ 和 TPL 的自訂資料分割器如何:實作動態資料分割

另請參閱

適用於

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

Source:
Parallel.cs
Source:
Parallel.cs
Source:
Parallel.cs

使用 64 位元索引在 foreach 上執行 For Each (在 Visual Basic 中為 IEnumerable) 運算,其中可以平行執行反覆項目、可以設定迴圈選項,還可以監控和操作迴圈的狀態。

public:
generic <typename TSource>
 static System::Threading::Tasks::ParallelLoopResult ForEach(System::Collections::Generic::IEnumerable<TSource> ^ source, System::Threading::Tasks::ParallelOptions ^ parallelOptions, Action<TSource, System::Threading::Tasks::ParallelLoopState ^, long> ^ body);
public static System.Threading.Tasks.ParallelLoopResult ForEach<TSource> (System.Collections.Generic.IEnumerable<TSource> source, System.Threading.Tasks.ParallelOptions parallelOptions, Action<TSource,System.Threading.Tasks.ParallelLoopState,long> body);
static member ForEach : seq<'Source> * System.Threading.Tasks.ParallelOptions * Action<'Source, System.Threading.Tasks.ParallelLoopState, int64> -> System.Threading.Tasks.ParallelLoopResult
Public Shared Function ForEach(Of TSource) (source As IEnumerable(Of TSource), parallelOptions As ParallelOptions, body As Action(Of TSource, ParallelLoopState, Long)) As ParallelLoopResult

類型參數

TSource

來源中的資料類型。

參數

source
IEnumerable<TSource>

可列舉的資料來源。

parallelOptions
ParallelOptions

物件,設定這個作業的行為。

body
Action<TSource,ParallelLoopState,Int64>

每個反覆項目都會叫用一次的委派。

傳回

包含迴圈已完成部分之相關資訊的的結構。

例外狀況

parallelOptions 引數中的 CancellationToken 已取消

source 引數為 null

-或-

parallelOptions 引數為 null

-或-

body 引數為 null

在所有執行緒擲回包含所有個別例外狀況的例外狀況。

已處置與 parallelOptions 中的 CancellationTokenSource 相關聯的 CancellationToken

備註

委派 body 會針對可列舉中的每個 source 專案叫用一次。 它隨附下列參數:目前專案、 ParallelLoopState 可用來提前中斷迴圈的實例,以及目前專案的索引 (Int64) 。

另請參閱

適用於

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

Source:
Parallel.cs
Source:
Parallel.cs
Source:
Parallel.cs

Partitioner 上執行 foreach (在 Visual Basic 中為 For Each) 運算,其中可以平行執行反覆項目,還可以監控和操作迴圈的狀態。

public:
generic <typename TSource>
 static System::Threading::Tasks::ParallelLoopResult ForEach(System::Collections::Concurrent::Partitioner<TSource> ^ source, Action<TSource, System::Threading::Tasks::ParallelLoopState ^> ^ body);
public static System.Threading.Tasks.ParallelLoopResult ForEach<TSource> (System.Collections.Concurrent.Partitioner<TSource> source, Action<TSource,System.Threading.Tasks.ParallelLoopState> body);
static member ForEach : System.Collections.Concurrent.Partitioner<'Source> * Action<'Source, System.Threading.Tasks.ParallelLoopState> -> System.Threading.Tasks.ParallelLoopResult
Public Shared Function ForEach(Of TSource) (source As Partitioner(Of TSource), body As Action(Of TSource, ParallelLoopState)) As ParallelLoopResult

類型參數

TSource

source 中的項目類型。

參數

source
Partitioner<TSource>

包含原始資料來源的 Partitioner。

body
Action<TSource,ParallelLoopState>

每個反覆項目都會叫用一次的委派。

傳回

包含迴圈已完成部分之相關資訊的的結構。

例外狀況

source 引數為 null

-或-

body 引數為 null

source Partitioner 中的 SupportsDynamicPartitions 屬性會傳回 false

-或-

source Partitioner 中的方法會傳回 null

-或-

source Partitioner 中的 GetPartitions(Int32) 方法未傳回正確的分割數目。

擲回的例外狀況包含從其中一個指定委派擲回的例外狀況。

備註

針對您想要覆寫預設資料分割配置的案例,會提供此多載。 例如,小型迴圈主體可能會受益于分割範圍。 方法 Parallel.ForEach 預期自訂資料分割器支援動態資料分割。 如需詳細資訊,請參閱 PLINQ 和 TPL 的自訂資料分割器如何:實作動態資料分割

另請參閱

適用於

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

Source:
Parallel.cs
Source:
Parallel.cs
Source:
Parallel.cs

foreach 上執行 For Each (在 Visual Basic 中為 Partitioner) 運算,其中可以平行執行反覆項目。

public:
generic <typename TSource>
 static System::Threading::Tasks::ParallelLoopResult ForEach(System::Collections::Concurrent::Partitioner<TSource> ^ source, Action<TSource> ^ body);
public static System.Threading.Tasks.ParallelLoopResult ForEach<TSource> (System.Collections.Concurrent.Partitioner<TSource> source, Action<TSource> body);
static member ForEach : System.Collections.Concurrent.Partitioner<'Source> * Action<'Source> -> System.Threading.Tasks.ParallelLoopResult
Public Shared Function ForEach(Of TSource) (source As Partitioner(Of TSource), body As Action(Of TSource)) As ParallelLoopResult

類型參數

TSource

source 中的項目類型。

參數

source
Partitioner<TSource>

包含原始資料來源的 Partitioner。

body
Action<TSource>

每個反覆項目都會叫用一次的委派。

傳回

包含迴圈已完成部分之相關資訊的的結構。

例外狀況

source 引數為 null

-或-

body 引數為 null

source Partitioner 中的 SupportsDynamicPartitions 屬性會傳回 false

-或-

source Partitioner 中的任何方法傳回 null 時,擲回的例外狀況。

-或-

source Partitioner 中的 GetPartitions(Int32) 方法未傳回正確的分割數目。

擲回的例外狀況包含從其中一個指定委派擲回的例外狀況。

範例

下列範例示範如何實作範圍分割器以搭配 Parallel.ForEach 使用:

using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;

class RangePartitionerDemo 
{
        static void Main()
        {
            Stopwatch sw = null;

            long sum = 0;
            long SUMTOP = 10000000;

            // Try sequential for
            sw = Stopwatch.StartNew();
            for (long i = 0; i < SUMTOP; i++) sum += i;
            sw.Stop();
            Console.WriteLine("sequential for result = {0}, time = {1} ms", sum, sw.ElapsedMilliseconds);

            // Try parallel for -- this is slow!
            //sum = 0;
            //sw = Stopwatch.StartNew();
            //Parallel.For(0L, SUMTOP, (item) => Interlocked.Add(ref sum, item));
            //sw.Stop();
            //Console.WriteLine("parallel for  result = {0}, time = {1} ms", sum, sw.ElapsedMilliseconds);

            // Try parallel for with locals
            sum = 0;
            sw = Stopwatch.StartNew();
            Parallel.For(0L, SUMTOP, () => 0L, (item, state, prevLocal) => prevLocal + item, local => Interlocked.Add(ref sum, local));
            sw.Stop();
            Console.WriteLine("parallel for w/locals result = {0}, time = {1} ms", sum, sw.ElapsedMilliseconds);

            // Try range partitioner
            sum = 0;
            sw = Stopwatch.StartNew();
            Parallel.ForEach(Partitioner.Create(0L, SUMTOP), (range) =>
            {
                long local = 0;
                for (long i = range.Item1; i < range.Item2; i++) local += i;
                Interlocked.Add(ref sum, local);
            });
            sw.Stop();
            Console.WriteLine("range partitioner result = {0}, time = {1} ms", sum, sw.ElapsedMilliseconds);
        }
}
Imports System.Collections.Concurrent
Imports System.Threading
Imports System.Threading.Tasks

Module RangePartitionerDemo

    Sub Main()
        Dim sw As Stopwatch = Nothing

        Dim sum As Long = 0
        Dim SUMTOP As Long = 10000000

        ' Try sequential for
        sw = Stopwatch.StartNew()
        For i As Long = 0 To SUMTOP - 1
            sum += i
        Next
        sw.Stop()
        Console.WriteLine("sequential for result = {0}, time = {1} ms", sum, sw.ElapsedMilliseconds)

        ' Try parallel for with locals
        sum = 0
        sw = Stopwatch.StartNew()
        Parallel.For(0L, SUMTOP, Function() 0L, Function(item, state, prevLocal) prevLocal + item, Function(local) Interlocked.Add(sum, local))
        sw.Stop()
        Console.WriteLine("parallel for w/locals result = {0}, time = {1} ms", sum, sw.ElapsedMilliseconds)

        ' Try range partitioner
        sum = 0
        sw = Stopwatch.StartNew()
        Parallel.ForEach(Partitioner.Create(0L, SUMTOP),
                         Sub(range)
                             Dim local As Long = 0
                             For i As Long = range.Item1 To range.Item2 - 1
                                 local += i
                             Next
                             Interlocked.Add(sum, local)
                         End Sub)
        sw.Stop()
        Console.WriteLine("range partitioner result = {0}, time = {1} ms", sum, sw.ElapsedMilliseconds)
    End Sub

End Module

備註

針對您想要覆寫預設資料分割配置的案例,會提供此多載。 例如,小型迴圈主體可能會受益于分割範圍。 方法 Parallel.ForEach 預期自訂資料分割器支援動態資料分割。 如需詳細資訊,請參閱 PLINQ 和 TPL 的自訂資料分割器如何:實作動態資料分割

另請參閱

適用於

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

Source:
Parallel.cs
Source:
Parallel.cs
Source:
Parallel.cs

使用 64 位元索引在 IEnumerable 上執行 foreach (在 Visual Basic 中為 For Each) 運算,其中可以平行執行反覆項目,還可以監控和操作迴圈的狀態。

public:
generic <typename TSource>
 static System::Threading::Tasks::ParallelLoopResult ForEach(System::Collections::Generic::IEnumerable<TSource> ^ source, Action<TSource, System::Threading::Tasks::ParallelLoopState ^, long> ^ body);
public static System.Threading.Tasks.ParallelLoopResult ForEach<TSource> (System.Collections.Generic.IEnumerable<TSource> source, Action<TSource,System.Threading.Tasks.ParallelLoopState,long> body);
static member ForEach : seq<'Source> * Action<'Source, System.Threading.Tasks.ParallelLoopState, int64> -> System.Threading.Tasks.ParallelLoopResult
Public Shared Function ForEach(Of TSource) (source As IEnumerable(Of TSource), body As Action(Of TSource, ParallelLoopState, Long)) As ParallelLoopResult

類型參數

TSource

來源中的資料類型。

參數

source
IEnumerable<TSource>

可列舉的資料來源。

body
Action<TSource,ParallelLoopState,Int64>

每個反覆項目都會叫用一次的委派。

傳回

包含迴圈已完成部分之相關資訊的的結構。

例外狀況

source 引數為 null

-或-

body 引數為 null

在所有執行緒擲回包含所有個別例外狀況的例外狀況。

備註

委派 body 會針對可列舉中的每個 source 專案叫用一次。 它隨附下列參數:目前專案、 ParallelLoopState 可用來提前中斷迴圈的實例,以及目前專案的索引 (Int64) 。

另請參閱

適用於

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

Source:
Parallel.cs
Source:
Parallel.cs
Source:
Parallel.cs

foreach 上執行 For Each (在 Visual Basic 中為 OrderablePartitioner<TSource>) 運算,其中可以平行執行反覆項目,還可以監控和操作迴圈的狀態。

public:
generic <typename TSource>
 static System::Threading::Tasks::ParallelLoopResult ForEach(System::Collections::Concurrent::OrderablePartitioner<TSource> ^ source, Action<TSource, System::Threading::Tasks::ParallelLoopState ^, long> ^ body);
public static System.Threading.Tasks.ParallelLoopResult ForEach<TSource> (System.Collections.Concurrent.OrderablePartitioner<TSource> source, Action<TSource,System.Threading.Tasks.ParallelLoopState,long> body);
static member ForEach : System.Collections.Concurrent.OrderablePartitioner<'Source> * Action<'Source, System.Threading.Tasks.ParallelLoopState, int64> -> System.Threading.Tasks.ParallelLoopResult
Public Shared Function ForEach(Of TSource) (source As OrderablePartitioner(Of TSource), body As Action(Of TSource, ParallelLoopState, Long)) As ParallelLoopResult

類型參數

TSource

source 中的項目類型。

參數

source
OrderablePartitioner<TSource>

包含原始資料來源的可排序 Partitioner。

body
Action<TSource,ParallelLoopState,Int64>

每個反覆項目都會叫用一次的委派。

傳回

包含迴圈已完成部分之相關資訊的的結構。

例外狀況

source 引數為 null

-或-

body 引數為 null

source 可排序 Partitioner 中的 SupportsDynamicPartitions 屬性會傳回 false

-或-

來源可排序 Partitioner 中的 KeysNormalized 屬性會傳回 false

-或-

在來源可排序的 Partitioner 中的任何方法都會傳回null

從其中一個指定委派擲回的例外狀況。

備註

針對您想要覆寫預設資料分割配置的案例,會提供此多載。 例如,小型迴圈主體可能會受益于分割範圍。 方法 Parallel.ForEach 預期自訂資料分割器支援動態資料分割。 如需詳細資訊,請參閱 PLINQ 和 TPL 的自訂資料分割器如何:實作動態資料分割

另請參閱

適用於

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

Source:
Parallel.cs
Source:
Parallel.cs
Source:
Parallel.cs

foreach 上執行 For Each (在 Visual Basic 中為 IEnumerable) 運算,其中可以平行執行反覆項目。

public:
generic <typename TSource>
 static System::Threading::Tasks::ParallelLoopResult ForEach(System::Collections::Generic::IEnumerable<TSource> ^ source, Action<TSource> ^ body);
public static System.Threading.Tasks.ParallelLoopResult ForEach<TSource> (System.Collections.Generic.IEnumerable<TSource> source, Action<TSource> body);
static member ForEach : seq<'Source> * Action<'Source> -> System.Threading.Tasks.ParallelLoopResult
Public Shared Function ForEach(Of TSource) (source As IEnumerable(Of TSource), body As Action(Of TSource)) As ParallelLoopResult

類型參數

TSource

來源中的資料類型。

參數

source
IEnumerable<TSource>

可列舉的資料來源。

body
Action<TSource>

每個反覆項目都會叫用一次的委派。

傳回

包含迴圈已完成部分之相關資訊的的結構。

例外狀況

source 引數為 null

-或-

body 引數為 null

在所有執行緒擲回包含所有個別例外狀況的例外狀況。

範例

下列範例會 ForEach<TSource>(IEnumerable<TSource>, Action<TSource>) 使用 方法來計算文字檔中的體音和非空白字元數目。 在此情況下, ParallelLoopResult 會忽略 方法傳回的值。 請注意,因為作業可以平行執行,所以您必須確定遞增計數器變數是不可部分完成的作業,而且多個執行緒不會同時嘗試存取計數器變數。 針對此目的,此範例會 lock 使用 C#) 中的 語句 (,以及 SyncLock Visual Basic) 中的 語句 (。

using System;
using System.IO;
using System.Threading.Tasks;

public class Example
{
   public static void Main()
   {
      Task<String> task = ReadCharacters(@".\CallOfTheWild.txt");
      String text = task.Result;
      
      int nVowels = 0;
      int nNonWhiteSpace = 0;
      Object obj = new Object();

      ParallelLoopResult result = Parallel.ForEach(text, 
                                                   (ch) => {
                                                      Char uCh = Char.ToUpper(ch);
                                                      if ("AEIOUY".IndexOf(uCh) >= 0) {
                                                         lock (obj) {
                                                            nVowels++;
                                                         }
                                                      }
                                                      if (! Char.IsWhiteSpace(uCh)) {
                                                         lock (obj) {
                                                            nNonWhiteSpace++;
                                                         }   
                                                      }
                                                   } );
      Console.WriteLine("Total characters:      {0,10:N0}", text.Length);
      Console.WriteLine("Total vowels:          {0,10:N0}", nVowels);
      Console.WriteLine("Total non-white-space:  {0,10:N0}", nNonWhiteSpace);
   }

   private static async Task<String> ReadCharacters(String fn)
   {
      String text;
      using (StreamReader sr = new StreamReader(fn)) {
         text = await sr.ReadToEndAsync();
      }
      return text;
   }
}
// The example displays output like the following:
//       Total characters:         198,548
//       Total vowels:              58,421
//       Total non-white-space:     159,461
Imports System.IO
Imports System.Threading.Tasks

Module Example
   Public Sub Main()
      Dim task As Task(Of String) = ReadCharacters(".\CallOfTheWild.txt")
      Dim text As String = task.Result
      
      Dim nVowels As Integer = 0
      Dim nNonWhiteSpace As Integer = 0
      Dim obj As New Object()

      Dim result As ParallelLoopResult = Parallel.ForEach(text, 
                                                          Sub(ch)
                                                             Dim uCh As Char = Char.ToUpper(ch)
                                                             If "AEIOUY".IndexOf(uCh) >= 0 Then
                                                                SyncLock obj
                                                                   nVowels += 1
                                                                End SyncLock
                                                             End If
                                                             If Not Char.IsWhiteSpace(uCh) Then
                                                                SyncLock obj
                                                                   nNonWhiteSpace += 1
                                                                End SyncLock   
                                                             End If
                                                          End Sub)
      Console.WriteLine("Total characters:      {0,10:N0}", text.Length)
      Console.WriteLine("Total vowels:          {0,10:N0}", nVowels)
      Console.WriteLine("Total non-white space:  {0,10:N0}", nNonWhiteSpace)
   End Sub
   
   Private Async Function ReadCharacters(fn As String) As Task(Of String)
      Dim text As String
      Using sr As New StreamReader(fn)
         text = Await sr.ReadToEndAsync()
      End Using
      Return text
   End Function
End Module
' The output from the example resembles the following:
'       Total characters:         198,548
'       Total vowels:              58,421
'       Total non-white space:     159,461

備註

委派 body 會針對可列舉中的每個 source 專案叫用一次。 它會以目前專案做為參數提供。

另請參閱

適用於

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

Source:
Parallel.cs
Source:
Parallel.cs
Source:
Parallel.cs

foreach 上執行 For Each (在 Visual Basic 中為 Partitioner) 運算,其中可以平行執行反覆項目、可以設定迴圈選項,還可以監控和操作迴圈的狀態。

public:
generic <typename TSource>
 static System::Threading::Tasks::ParallelLoopResult ForEach(System::Collections::Concurrent::Partitioner<TSource> ^ source, System::Threading::Tasks::ParallelOptions ^ parallelOptions, Action<TSource, System::Threading::Tasks::ParallelLoopState ^> ^ body);
public static System.Threading.Tasks.ParallelLoopResult ForEach<TSource> (System.Collections.Concurrent.Partitioner<TSource> source, System.Threading.Tasks.ParallelOptions parallelOptions, Action<TSource,System.Threading.Tasks.ParallelLoopState> body);
static member ForEach : System.Collections.Concurrent.Partitioner<'Source> * System.Threading.Tasks.ParallelOptions * Action<'Source, System.Threading.Tasks.ParallelLoopState> -> System.Threading.Tasks.ParallelLoopResult
Public Shared Function ForEach(Of TSource) (source As Partitioner(Of TSource), parallelOptions As ParallelOptions, body As Action(Of TSource, ParallelLoopState)) As ParallelLoopResult

類型參數

TSource

source 中的項目類型。

參數

source
Partitioner<TSource>

包含原始資料來源的 Partitioner。

parallelOptions
ParallelOptions

物件,設定這個作業的行為。

body
Action<TSource,ParallelLoopState>

每個反覆項目都會叫用一次的委派。

傳回

包含迴圈已完成部分之相關資訊的的結構。

例外狀況

parallelOptions 引數中的 CancellationToken 已取消。

已處置與 parallelOptions 中的 CancellationTokenSource 相關聯的 CancellationToken

source 引數為 null

-或-

parallelOptions 引數為 null

-或-

body 引數為 null

source Partitioner 中的 SupportsDynamicPartitions 屬性會傳回 false

-或-

source Partitioner 中的任何方法傳回 null 時,擲回的例外狀況。

擲回的例外狀況包含從其中一個指定委派擲回的例外狀況。

備註

針對您想要覆寫預設資料分割配置的案例,會提供此多載。 例如,小型迴圈主體可能會受益于分割範圍。 方法 Parallel.ForEach 預期自訂資料分割器支援動態資料分割。 如需詳細資訊,請參閱 PLINQ 和 TPL 的自訂資料分割器如何:實作動態資料分割

另請參閱

適用於

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

Source:
Parallel.cs
Source:
Parallel.cs
Source:
Parallel.cs

IEnumerable 上執行 foreach (在 Visual Basic 中為 For Each) 運算,其中可以平行執行反覆項目,還可以監控和操作迴圈的狀態。

public:
generic <typename TSource>
 static System::Threading::Tasks::ParallelLoopResult ForEach(System::Collections::Generic::IEnumerable<TSource> ^ source, Action<TSource, System::Threading::Tasks::ParallelLoopState ^> ^ body);
public static System.Threading.Tasks.ParallelLoopResult ForEach<TSource> (System.Collections.Generic.IEnumerable<TSource> source, Action<TSource,System.Threading.Tasks.ParallelLoopState> body);
static member ForEach : seq<'Source> * Action<'Source, System.Threading.Tasks.ParallelLoopState> -> System.Threading.Tasks.ParallelLoopResult
Public Shared Function ForEach(Of TSource) (source As IEnumerable(Of TSource), body As Action(Of TSource, ParallelLoopState)) As ParallelLoopResult

類型參數

TSource

來源中的資料類型。

參數

source
IEnumerable<TSource>

可列舉的資料來源。

body
Action<TSource,ParallelLoopState>

每個反覆項目都會叫用一次的委派。

傳回

包含迴圈已完成部分之相關資訊的的結構。

例外狀況

source 引數為 null

-或-

body 引數為 null

在所有執行緒擲回包含所有個別例外狀況的例外狀況。

備註

委派 body 會針對可列舉中的每個 source 專案叫用一次。 它隨附下列參數:目前的 元素,以及 ParallelLoopState 可用來提前中斷迴圈的 實例。

另請參閱

適用於