Parallel.ForEach 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
執行一個 foreach(Visual Basic 中稱為 For Each )操作,且可平行執行迭代。
多載
ForEach<TSource,TLocal>(IEnumerable<TSource>, ParallelOptions, Func<TLocal>, Func<TSource,ParallelLoopState,TLocal,TLocal>, Action<TLocal>)
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- 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。
CancellationToken
parallelOptions爭論被取消了。
CancellationTokenSource與相關CancellationTokenparallelOptions事物已處置。
包含所有執行緒拋出的個別例外的例外。
備註
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>)
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
在 IEnumerable 上執行 foreach(Visual Basic 中為 For Each)操作,使用執行緒本地資料與 64 位元索引,該操作可平行執行,可配置迴圈選項,並監控與操作迴圈狀態。
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。
CancellationToken
parallelOptions爭論被取消了。
CancellationTokenSource與相關CancellationTokenparallelOptions事物已處置。
包含所有執行緒拋出的個別例外的例外。
備註
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>)
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
在 Visual BasicPartitioner 上執行 foreach(For Each)操作,並以執行緒本地資料,可平行執行迭代,可設定迴圈選項,並監控與操作迴圈狀態。
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>
包含原始資料來源的分割器。
- parallelOptions
- ParallelOptions
一個用來設定此操作行為的物件。
- localInit
- Func<TLocal>
函式委派每個任務的本地資料初始狀態。
- body
- Func<TSource,ParallelLoopState,TLocal,TLocal>
這個代理在每次迭代中被調用一次。
- localFinally
- Action<TLocal>
代表負責對每個任務的本地狀態執行最後操作。
傳回
一個包含迴圈完成部分資訊的結構。
例外狀況
論 source 點為 null。
-或-
論 parallelOptions 點為 null。
-或-
論 body 點為 null。
-或-
論 localInit 點為 null。
-或-
論 localFinally 點為 null。
SupportsDynamicPartitions返回false器中的Partitionersource屬性或分割器回傳null分割區。
包含所有執行緒拋出的個別例外的例外。
CancellationToken
parallelOptions爭論被取消了。
CancellationTokenSource與相關CancellationTokenparallelOptions事物已處置。
備註
這種過載是為了你想覆寫預設分割方案的情況而提供。 例如,小型迴圈體可能從分隔範圍中受益。 此 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>)
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
執行 foreach(Visual BasicFor Each)操作,採用 64 位元索引及執行緒本地資料,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
執行緒本地資料的類型。
參數
包含原始資料來源的可排序分割器。
- parallelOptions
- ParallelOptions
一個用來設定此操作行為的物件。
- localInit
- Func<TLocal>
函式委派每個任務的本地資料初始狀態。
- body
- Func<TSource,ParallelLoopState,Int64,TLocal,TLocal>
這個代理在每次迭代中被調用一次。
- localFinally
- Action<TLocal>
代表負責對每個任務的本地狀態執行最後操作。
傳回
一個包含迴圈完成部分資訊的結構。
例外狀況
論 source 點為 null。
-或-
論 parallelOptions 點為 null。
-或-
論 body 點為 null。
-或-
或localInitlocalFinally參數為 null。
SupportsDynamicPartitions返回false器中的Partitionersource屬性或分割器回傳null分割區。
包含所有執行緒拋出的個別例外的例外。
CancellationToken
parallelOptions爭論被取消了。
CancellationTokenSource與相關CancellationTokenparallelOptions事物已處置。
備註
這種過載是為了你想覆寫預設分割方案的情況而提供。 例如,小型迴圈體可能從分隔範圍中受益。 此 ForEach 方法期望自訂分割器支援動態分割。 欲了解更多資訊,請參閱《 PLINQ 與 TPL 的自訂分割器 》及 《如何實作動態分割區》。
localInit代理會對參與迴圈執行的每個任務呼叫一次,並回傳每個任務的初始本地狀態。 這些初始狀態會傳遞給每個任務的首次 body 呼叫。 接著,每次後續的身體呼叫都會回傳一個可能修改過的狀態值,並傳給下一個身體呼叫。 最後,每個執行緒的最後一次主體調用會回傳一個狀態值,並傳給 localFinally 代理。
localFinally每個任務會被召喚一次,對每個任務的當地狀態執行最終行動。 此代表可同時執行多項任務;因此,你必須同步存取任何共享變數。
該 Parallel.ForEach 方法在其執行期間可能使用比執行緒更多的任務,因為現有任務會完成並被新任務取代。 這讓底層 TaskScheduler 物件有機會新增、變更或移除服務迴圈的執行緒。
適用於
ForEach<TSource,TLocal>(IEnumerable<TSource>, Func<TLocal>, Func<TSource,ParallelLoopState,TLocal,TLocal>, Action<TLocal>)
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
在 Visual BasicIEnumerable 上執行 foreach(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 ^, 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>)
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- 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>)
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
在 Partitioner 上執行 foreach(Visual Basic 中為 For Each)操作,該操作可平行迭代,並監控與操作迴圈狀態。
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>
包含原始資料來源的分割器。
- localInit
- Func<TLocal>
函式委派每個任務的本地資料初始狀態。
- body
- Func<TSource,ParallelLoopState,TLocal,TLocal>
這個代理在每次迭代中被調用一次。
- localFinally
- Action<TLocal>
代表負責對每個任務的本地狀態執行最後操作。
傳回
一個包含迴圈完成部分資訊的結構。
例外狀況
論 source 點為 null。
-或-
論 body 點為 null。
-或-
論 localInit 點為 null。
-或-
論 localFinally 點為 null。
SupportsDynamicPartitions返回false器中的Partitionersource屬性或分割器回傳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>)
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
在 Visual BasicOrderablePartitioner<TSource> 上執行 foreach(For Each)操作,並以執行緒本地資料,可平行執行迭代,可設定迴圈選項,並監控與操作迴圈狀態。
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
執行緒本地資料的類型。
參數
包含原始資料來源的可排序分割器。
- localInit
- Func<TLocal>
函式委派每個任務的本地資料初始狀態。
- body
- Func<TSource,ParallelLoopState,Int64,TLocal,TLocal>
這個代理在每次迭代中被調用一次。
- localFinally
- Action<TLocal>
代表負責對每個任務的本地狀態執行最後操作。
傳回
一個包含迴圈完成部分資訊的結構。
例外狀況
論 source 點為 null。
-或-
論 body 點為 null。
-或-
論 localInit 點為 null。
-或-
論 localFinally 點為 null。
SupportsDynamicPartitions返回false器中的Partitionersource屬性或分割器回傳null分割區。
包含所有執行緒拋出的個別例外的例外。
備註
這種過載是為了你想覆寫預設分割方案的情況而提供。 例如,小型迴圈體可能從分隔範圍中受益。 此 ForEach 方法期望自訂分割器支援動態分割。 欲了解更多資訊,請參閱《 PLINQ 與 TPL 的自訂分割器 》及 《如何實作動態分割區》。
localInit代理會對參與迴圈執行的每個任務呼叫一次,並回傳每個任務的初始本地狀態。 這些初始狀態會傳遞給每個任務的首次 body 呼叫。 接著,每次後續的身體呼叫都會回傳一個可能修改過的狀態值,並傳給下一個身體呼叫。 最後,每個任務的最後一次主體調用會回傳一個狀態值,並傳給 localFinally 代理。
localFinally每個任務會被召喚一次,對每個任務的當地狀態執行最終行動。 此代表可同時執行多項任務;因此,你必須同步存取任何共享變數。
該 Parallel.ForEach 方法在其執行期間可能使用比執行緒更多的任務,因為現有任務會完成並被新任務取代。 這讓底層 TaskScheduler 物件有機會新增、變更或移除服務迴圈的執行緒。
適用於
ForEach<TSource>(IEnumerable<TSource>, ParallelOptions, Action<TSource>)
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- 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 被取消
包含所有執行緒拋出的個別例外的例外。
CancellationTokenSource與相關CancellationTokenparallelOptions事物已處置。
備註
body代理對可枚舉元素的每個元素source都會被調用一次。 它以當前元素作為參數提供。
適用於
ForEach<TSource>(IEnumerable<TSource>, ParallelOptions, Action<TSource,ParallelLoopState>)
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- 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 被取消
包含所有執行緒拋出的個別例外的例外。
CancellationTokenSource與相關CancellationTokenparallelOptions事物已處置。
備註
body代理對可枚舉元素的每個元素source都會被調用一次。 它提供以下參數:目前元素,以及 ParallelLoopState 一個可用來提前脫離迴圈的實例。
適用於
ForEach<TSource>(OrderablePartitioner<TSource>, ParallelOptions, Action<TSource,ParallelLoopState,Int64>)
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
對一個OrderablePartitioner<TSource>執行foreach(Visual Basic 中For Each)操作,該操作可平行執行,迴圈選項可配置,迴圈狀態可被監控與操作。
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。
參數
包含原始資料來源的可排序分割器。
- parallelOptions
- ParallelOptions
一個用來設定此操作行為的物件。
- body
- Action<TSource,ParallelLoopState,Int64>
這個代理在每次迭代中被調用一次。
傳回
一個包含迴圈完成部分資訊的結構。
例外狀況
在論parallelOptions證中,該 CancellationToken 被取消
CancellationTokenSource與相關CancellationTokenparallelOptions事物已處置。
SupportsDynamicPartitions可排序分割器中的source屬性會回傳 false。
-或-
KeysNormalized可排序分割器中的source屬性會回傳 false。
-或-
當可排序分割器中 source 任何方法回傳 null時拋出的例外。
被拋出的例外包含來自指定代理之一的例外。
備註
這種過載是為了你想覆寫預設分割方案的情況而提供。 例如,小型迴圈體可能從分隔範圍中受益。 此 Parallel.ForEach 方法期望自訂分割器支援動態分割。 欲了解更多資訊,請參閱《 PLINQ 與 TPL 的自訂分割器 》及 《如何實作動態分割區》。
適用於
ForEach<TSource>(Partitioner<TSource>, ParallelOptions, Action<TSource>)
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- 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>
包含原始資料來源的分割器。
- parallelOptions
- ParallelOptions
一個用來設定此操作行為的物件。
- body
- Action<TSource>
這個代理在每次迭代中被調用一次。
傳回
一個包含迴圈完成部分資訊的結構。
例外狀況
CancellationToken
parallelOptions爭論被取消了。
CancellationTokenSource與相關CancellationTokenparallelOptions事物已處置。
被拋出的例外包含來自指定代理之一的例外。
備註
這種過載是為了你想覆寫預設分割方案的情況而提供。 例如,小型迴圈體可能從分隔範圍中受益。 此 Parallel.ForEach 方法期望自訂分割器支援動態分割。 欲了解更多資訊,請參閱《 PLINQ 與 TPL 的自訂分割器 》及 《如何實作動態分割區》。
適用於
ForEach<TSource>(IEnumerable<TSource>, ParallelOptions, Action<TSource,ParallelLoopState,Int64>)
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- 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 ^, 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 被取消
包含所有執行緒拋出的個別例外的例外。
CancellationTokenSource與相關CancellationTokenparallelOptions事物已處置。
備註
body代理對可枚舉元素的每個元素source都會被調用一次。 它提供以下參數:目前元素、 ParallelLoopState 可用來提前脫離迴圈的實例,以及當前元素的索引(Int64)。
適用於
ForEach<TSource>(Partitioner<TSource>, Action<TSource,ParallelLoopState>)
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- 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>
包含原始資料來源的分割器。
- body
- Action<TSource,ParallelLoopState>
這個代理在每次迭代中被調用一次。
傳回
一個包含迴圈完成部分資訊的結構。
例外狀況
分割器中的屬性會SupportsDynamicPartitions回傳 false。source
-或-
分割器中的 source 一個方法會回傳 null。
-或-
GetPartitions(Int32)分割器中的source方法無法回傳正確的分割數量。
被拋出的例外包含來自指定代理之一的例外。
備註
這種過載是為了你想覆寫預設分割方案的情況而提供。 例如,小型迴圈體可能從分隔範圍中受益。 此 Parallel.ForEach 方法期望自訂分割器支援動態分割。 欲了解更多資訊,請參閱《 PLINQ 與 TPL 的自訂分割器 》及 《如何實作動態分割區》。
適用於
ForEach<TSource>(Partitioner<TSource>, Action<TSource>)
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
執行一個 foreach(Visual Basic 中為 For Each)操作,該操作可平行執行的 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>
包含原始資料來源的分割器。
- body
- Action<TSource>
這個代理在每次迭代中被調用一次。
傳回
一個包含迴圈完成部分資訊的結構。
例外狀況
分割器中的屬性會SupportsDynamicPartitions回傳 false。source
-或-
當分區器中 source 任何方法回傳 null時拋出的例外。
-或-
GetPartitions(Int32)分割器中的source方法無法回傳正確的分割數量。
被拋出的例外包含來自指定代理之一的例外。
範例
以下範例展示了如何實作用於以下 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>)
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- 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 ^, 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>
這個代理在每次迭代中被調用一次。
傳回
一個包含迴圈完成部分資訊的結構。
例外狀況
包含所有執行緒拋出的個別例外的例外。
備註
body代理對可枚舉元素的每個元素source都會被調用一次。 它提供以下參數:目前元素、 ParallelLoopState 可用來提前脫離迴圈的實例,以及當前元素的索引(Int64)。
適用於
ForEach<TSource>(OrderablePartitioner<TSource>, Action<TSource,ParallelLoopState,Int64>)
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
執行一個 foreach(Visual Basic 中為 For Each)操作,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。
參數
包含原始資料來源的可排序分割器。
- body
- Action<TSource,ParallelLoopState,Int64>
這個代理在每次迭代中被調用一次。
傳回
一個包含迴圈完成部分資訊的結構。
例外狀況
SupportsDynamicPartitions可排序分割器中的source屬性會回傳 false。
-或-
KeysNormalized來源可排序分割器的屬性會回傳 false。
-或-
來源可排序分割器中的任何方法都會回傳 null。
例外來自指定代表之一。
備註
這種過載是為了你想覆寫預設分割方案的情況而提供。 例如,小型迴圈體可能從分隔範圍中受益。 此 Parallel.ForEach 方法期望自訂分割器支援動態分割。 欲了解更多資訊,請參閱《 PLINQ 與 TPL 的自訂分割器 》及 《如何實作動態分割區》。
適用於
ForEach<TSource>(IEnumerable<TSource>, Action<TSource>)
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
執行一個foreach(Visual Basic 中為 For Each)操作,該操作可平行執行的 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>
這個代理在每次迭代中被調用一次。
傳回
一個包含迴圈完成部分資訊的結構。
例外狀況
包含所有執行緒拋出的個別例外的例外。
範例
以下範例使用此 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>)
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- 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, 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>
包含原始資料來源的分割器。
- parallelOptions
- ParallelOptions
一個用來設定此操作行為的物件。
- body
- Action<TSource,ParallelLoopState>
這個代理在每次迭代中被調用一次。
傳回
一個包含迴圈完成部分資訊的結構。
例外狀況
CancellationToken
parallelOptions爭論被取消了。
CancellationTokenSource與相關CancellationTokenparallelOptions事物已處置。
被拋出的例外包含來自指定代理之一的例外。
備註
這種過載是為了你想覆寫預設分割方案的情況而提供。 例如,小型迴圈體可能從分隔範圍中受益。 此 Parallel.ForEach 方法期望自訂分割器支援動態分割。 欲了解更多資訊,請參閱《 PLINQ 與 TPL 的自訂分割器 》及 《如何實作動態分割區》。
適用於
ForEach<TSource>(IEnumerable<TSource>, Action<TSource,ParallelLoopState>)
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
執行一個 foreach(Visual Basic 中為 For Each)操作,IEnumerable 可平行執行,迴圈狀態可被監控與操作。
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>
這個代理在每次迭代中被調用一次。
傳回
一個包含迴圈完成部分資訊的結構。
例外狀況
包含所有執行緒拋出的個別例外的例外。
備註
body代理對可枚舉元素的每個元素source都會被調用一次。 它提供以下參數:目前元素,以及 ParallelLoopState 一個可用來提前脫離迴圈的實例。