Perform Simple Parallel Tasks - CSharp Thread Asynchronous

CSharp examples for Thread Asynchronous:Parallel

Description

Perform Simple Parallel Tasks

Demo Code


using System;/*  w  w  w  .j ava 2s.  co  m*/
using System.Threading;
using System.Threading.Tasks;

class MainClass
    {
        static void Main(string[] args)
        {
            Parallel.Invoke(
                new Action(writeDays),
                new Action(writeMonths),
                new Action(writeCities)
            );
            Thread.Sleep(5000);
        }

       static void writeDays()
        {
            string[] daysArray = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
            foreach (string day in daysArray)
            {
                Console.WriteLine("Day of the Week: {0}", day);
                Thread.Sleep(500);
            }
        }

        static void writeMonths()
        {
            string[] monthsArray = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
            foreach (string month in monthsArray)
            {
                Console.WriteLine("Month: {0}", month);
                Thread.Sleep(500);
            }
        }

        static void writeCities()
        {
            string[] citiesArray = { "London", "New York", "Paris", "Tokyo", "Sydney" };
            foreach (string city in citiesArray)
            {
                Console.WriteLine("City: {0}", city);
                Thread.Sleep(500);
            }

        }
    }

Result


Related Tutorials