C# Reference ยท Async and Concurrency
Parallel.For and Parallel.ForEach
Reference for Parallel.For, Parallel.ForEach and Parallel.Invoke in C#: run loop bodies on several cores, limit parallelism, break early and aggregate results.
Parallel.For and Parallel.ForEach run the iterations of a loop on several threads of the thread pool at once. They block the calling thread until every iteration has finished. They are meant for CPU-bound work where iterations are independent: image filters, numeric computation, parsing in memory.
Parallel loops are not magic. Each iteration must not depend on the others, shared state needs synchronization, and the body has to be heavy enough to pay for the scheduling overhead. For I/O-bound work use Parallel.ForEachAsync or Task.WhenAll instead.
Parallel.For(0, 100, i => Process(i)); Parallel.ForEach(items, item => Handle(item));
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
public static class Parallel { public static ParallelLoopResult For(int fromInclusive, int toExclusive, Action<int> body); public static ParallelLoopResult For(int fromInclusive, int toExclusive, Action<int, ParallelLoopState> body); public static ParallelLoopResult For(int fromInclusive, int toExclusive, ParallelOptions parallelOptions, Action<int> body); public static ParallelLoopResult For<TLocal>(int fromInclusive, int toExclusive, Func<TLocal> localInit, Func<int, ParallelLoopState, TLocal, TLocal> body, Action<TLocal> localFinally); public static ParallelLoopResult ForEach<TSource>(IEnumerable<TSource> source, Action<TSource> body); public static ParallelLoopResult ForEach<TSource>(IEnumerable<TSource> source, ParallelOptions parallelOptions, Action<TSource> body); public static void Invoke(params Action[] actions); }
| Name | Description |
|---|---|
fromInclusive, toExclusive |
The index range, like a for loop (there is also a long overload). |
body |
The code run per iteration; it may receive a ParallelLoopState for Stop, Break and IsStopped. |
parallelOptions |
MaxDegreeOfParallelism, CancellationToken and TaskScheduler. |
localInit, localFinally |
Create and merge per-worker state, so the body needs no lock. |
Returns: a ParallelLoopResult with IsCompleted and LowestBreakIteration. Invoke returns nothing.
Errors: exceptions from bodies are gathered into one AggregateException thrown after the running iterations end; the loop stops starting new ones. A canceled token throws OperationCanceledException.
Usage notes
- No ordering guarantee. Iterations run in any order and at the same time. Do not print inside the loop and expect a stable sequence; collect results by index or sort them afterwards.
- Shared state needs protection. Writing to a
List<T>or incrementing a sharedintfrom the body is a race. Use disjoint array slots,Interlocked, a concurrent collection, or the thread-local overload. - Small bodies are slower in parallel. Scheduling costs more than adding two numbers. Measure; partition work into chunks (
Partitioner.Create) if needed. Stop()ends as soon as possible,Break()finishes everything before the current index. In both cases iterations already running complete.- The calling thread participates and is blocked until the loop ends. Do not call these loops on a UI thread or inside request handlers without thought.
- Not for
asyncbodies.Parallel.For(0, n, async i => await ...)compiles to anasync voidlambda, so the loop does not wait for the work and exceptions crash the process. Use Parallel.ForEachAsync. - Default parallelism follows the processor count; on a single-core machine (like our sandbox) work still runs correctly, just not faster.
- PLINQ (
AsParallel()) is the query-style alternative.