Export (0) Print
Expand All

Parallel.For Method (Int32, Int32, Action<Int32>)

Executes a for (For in Visual Basic) loop in which iterations may run in parallel.

Namespace:  System.Threading.Tasks
Assemblies:   mscorlib (in mscorlib.dll)
  System.Threading.Tasks.Parallel (in System.Threading.Tasks.Parallel.dll)

public static ParallelLoopResult For(
	int fromInclusive,
	int toExclusive,
	Action<int> body
)

Parameters

fromInclusive
Type: System.Int32

The start index, inclusive.

toExclusive
Type: System.Int32

The end index, exclusive.

body
Type: System.Action<Int32>

The delegate that is invoked once per iteration.

Return Value

Type: System.Threading.Tasks.ParallelLoopResult
A structure that contains information about which portion of the loop completed.

ExceptionCondition
ArgumentNullException

The body argument is null.

AggregateException

The exception that contains all the individual exceptions thrown on all threads.

The body delegate is invoked once for each value in the iteration range (fromInclusive, toExclusive). It is provided with the iteration count (Int32) as a parameter.

If fromInclusive is greater than or equal to toExclusive, the method returns immediately without performing any iterations.

The following example uses the For method for 100 invocations of a delegate that generates random byte values and computes their sum.

using System;
using System.Threading.Tasks;

public class Example
{
   public static void Main()
   {
      ParallelLoopResult result = Parallel.For(0, 100, ctr => { Random rnd = new Random(ctr * 100000);
                                                                Byte[] bytes = new Byte[100];
                                                                rnd.NextBytes(bytes);
                                                                int sum = 0;
                                                                foreach(var byt in bytes)
                                                                    sum += byt;
                                                                Console.WriteLine("Iteration {0,2}: {1:N0}", ctr, sum);
                                                              });
      Console.WriteLine("Result: {0}", result.IsCompleted ? "Completed Normally" : 
                                                             String.Format("Completed to {0}", result.LowestBreakIteration));
   }
}
// The following is a portion of the output displayed by the example: 
//       Iteration  0: 12,509 
//       Iteration 50: 12,823 
//       Iteration 51: 11,275 
//       Iteration 52: 12,531 
//       Iteration  1: 13,007 
//       Iteration 53: 13,799 
//       Iteration  4: 12,945 
//       Iteration  2: 13,246 
//       Iteration 54: 13,008 
//       Iteration 55: 12,727 
//       Iteration 56: 13,223 
//       Iteration 57: 13,717 
//       Iteration  5: 12,679 
//       Iteration  3: 12,455 
//       Iteration 58: 12,669 
//       Iteration 59: 11,882 
//       Iteration  6: 13,167 
//       ... 
//       Iteration 92: 12,275 
//       Iteration 93: 13,282 
//       Iteration 94: 12,745 
//       Iteration 95: 11,957 
//       Iteration 96: 12,455 
//       Result: Completed Normally

.NET Framework

Supported in: 4.6, 4.5, 4

.NET Framework Client Profile

Supported in: 4

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

Supported in: Windows Phone 8.1

Windows Phone 8.1, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft