Parallel Process a Collection - CSharp Thread Asynchronous

CSharp examples for Thread Asynchronous:Parallel

Description

Parallel Process a Collection

Demo Code


using System;//  www .  j  a  v a 2 s  .  c  o m
using System.Threading;
using System.Threading.Tasks;

class MainClass
    {
        static void Main(string[] args)
        {
            int[] numbersArray = { 100, 200, 300 };
            ParallelOptions options = new ParallelOptions();
            options.MaxDegreeOfParallelism = 2;

            Parallel.ForEach(numbersArray, options, baseNumber => printNumbers(baseNumber));

            Console.WriteLine("Tasks Completed, Press Enter");
            Console.ReadLine();
        }

        static void printNumbers(int baseNumber)
        {
            for (int i = baseNumber, j = baseNumber + 10; i < j; i++)
            {
                Console.WriteLine("Number: {0}", i);
                Thread.Sleep(100);
            }
        }
    }

Result


Related Tutorials