Executes a for loop in which iterations may run in parallel. - CSharp System.Threading.Tasks

CSharp examples for System.Threading.Tasks:Parallel

Description

Executes a for loop in which iterations may run in parallel.

Demo Code

//  Copyright (c) Microsoft Corporation.  All rights reserved. 
using System.Threading.Tasks;
using System.Numerics;
using System.Collections.Generic;
using System;/* w  w  w.  ja  v  a2s  .c  o  m*/

public class Main{
        /// <summary>Executes a for loop in which iterations may run in parallel.</summary>
        /// <param name="fromInclusive">The start index, inclusive.</param>
        /// <param name="toExclusive">The end index, exclusive.</param>
        /// <param name="options">A System.Threading.Tasks.ParallelOptions instance that configures the behavior of this operation.</param>
        /// <param name="body">The delegate that is invoked once per iteration.</param>
        public static void For(BigInteger fromInclusive, BigInteger toExclusive, ParallelOptions options, Action<BigInteger> body)
        {
            // Determine how many iterations to run...
            var range = toExclusive - fromInclusive;

            // ... and run them.
            if (range <= 0)
            {
                // If there's nothing to do, bail
                return;
            }
                // Fast path
            else if (range <= Int64.MaxValue)
            {
                // If the range is within the realm of Int64, we'll delegate to Parallel.For's Int64 overloads.
                // Iterate from 0 to range, and then call the user-provided body with the scaled-back value.
                Parallel.For(0, (long)range, options, i => body(i + fromInclusive));
            }
                // Slower path
            else
            {
                // For a range larger than Int64.MaxValue, we'll rely on an enumerable of BigInteger.
                // We create a C# iterator that yields all of the BigInteger values in the requested range
                // and then ForEach over that range.
                Parallel.ForEach(Range(fromInclusive, toExclusive), options, body);
            }
        }
        /// <summary>Executes a for loop in which iterations may run in parallel.</summary>
        /// <param name="fromInclusive">The start index, inclusive.</param>
        /// <param name="toExclusive">The end index, exclusive.</param>
        /// <param name="body">The delegate that is invoked once per iteration.</param>
        public static void For(BigInteger fromInclusive, BigInteger toExclusive, Action<BigInteger> body)
        {
            For(fromInclusive, toExclusive, s_defaultParallelOptions, body);
        }
}

Related Tutorials