Home > DeveloperSection > Blogs > Parellel.ForEach Loop

Parellel.ForEach Loop


C# C# 
Ratings:
0 Comment(s)
 2288  View(s)
Rate this:

In this blog I will show you a small demonstration of Parellel.ForEach loop. You can take advantage of multi-core processors and execute foreach loops in parallel.  This works especially well when getting multiple chunks of data from external data sources such as a database, web service, or RESTful API.

Following example will show you implementation of Parellel.ForEach loop.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApplication1
{

    /// <summary>
    /// A user defined datatype to store information of
    /// student such as Name and ID.
    /// </summary>
    public class Student
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            List<Student> StudentRecords = GetStudentRecords();

            //Here we will display all records of student whose name start with A
            //Using Parellel.ForEach loop.

            Parallel.ForEach(StudentRecords, TempStudent =>
                {
                    if (TempStudent.Name.StartsWith("A"))
                    {
                        Console.WriteLine("Student ID : {0} , Name : {1}", TempStudent.ID, TempStudent.Name);
                    }
                });
        }


        /// <summary>
        /// Returns a junk records of collection of
        /// Student to perform operation.
        /// </summary>
        /// <returns></returns>
        private static List<Student> GetStudentRecords()
        {
            List<Student> StudentList = new List<Student>();

            StudentList.Add(new Student
            {
                ID = 1,
                Name = "Awadhendra"
            });

            StudentList.Add(new Student
            {
                ID = 2,
                Name = "Anuj"
            });

            StudentList.Add(new Student
            {
                ID = 3,
                Name = "Ravi"
            });

            StudentList.Add(new Student
            {
                ID = 4,
                Name = "Ashish"
            });

            StudentList.Add(new Student
            {
                ID = 5,
                Name = "Atul"
            });

            StudentList.Add(new Student
            {
                ID = 6,
                Name = "Shikha"
            });

            StudentList.Add(new Student
            {
                ID = 7,
                Name = "Pooja"
            });

            StudentList.Add(new Student
            {
                ID = 8,
                Name = "Amit"
            });

            StudentList.Add(new Student
            {
                ID = 9,
                Name = "Nidhi"
            });

            StudentList.Add(new Student
            {
                ID = 10,
                Name = "Neha"
            });

            return StudentList;
        }
    }
}

Output of following code snippet is as follows.

Student ID : 8 , Name : Amit

Student ID : 4 , Name : Ashish

Student ID : 5 , Name : Atul

Student ID : 1 , Name : Awadhendra

Student ID : 2 , Name : Anuj

Press any key to continue . . .



Don't want to miss updates? Please click the below button!

Follow MindStick