Blog

Identify source collection, thread local initializer, body, local aggregator if no exception is thrown

class Program {
static void Main(string[] args) {
int[] IntArray = { 14, 11, 26, 22, 19, 1, 1, 3 }; int Result = 0;

try {
Parallel.ForEach(
IntArray,
() => 0, (n, loopState, localResult) =>{
localResult += n;
Console.WriteLine(” Thread: {0} N={1}, localResult={2}”, Thread.CurrentThread.ManagedThreadId, n, localResult);
return localResult;
},
(localResult) => Interlocked.Add(ref Result, localResult) );
Console.WriteLine(” Result={0}”, Result);
}catch (AggregateException e) { throw e; }

}
}