Create two threads, one display odd & other even numbers

using System;
using System.Threading;
namespace EvenOddUsingTwoThread
{
    class Program
    {
        static EventWaitHandle evenReady;
        static EventWaitHandle oddReady;

        static void Main(string[] args)
        {
            bool countOdd = true;
            bool countEven = true;

            if (countOdd && countEven)
            {
                evenReady = new AutoResetEvent(false);
                oddReady = new AutoResetEvent(true); // Must be true for the starting thread.
            }
            else
            {
                /*  The ManualResetEvent is the door, which needs to be closed(reset) manually.
                    The AutoResetEvent is a tollbooth, allowing one car to go by and automatically 
                    closing before the next one can get through.
                */
                evenReady = new ManualResetEvent(true);
                oddReady = new ManualResetEvent(true);
            }

            Thread countThreadOdd = new Thread(oddThread);
            Thread countThreadEven = new Thread(evenThread);

            //Thread Start
            if (countOdd)
                countThreadOdd.Start();

            if (countEven)
                countThreadEven.Start();

            //main thread will untill below thread is in exection mode

            if (countOdd)
                countThreadOdd.Join();

            if (countEven)
                countThreadEven.Join();

            Console.WriteLine("Done");
            Console.ReadLine();
        }

        public static void oddThread()
        {
            for (int i = 1; i < 10; i += 2)
            {
                evenReady.WaitOne();
                Console.WriteLine(i);
                oddReady.Set();
            }
        }

        public static void evenThread()
        {
            for (int i = 0; i < 10; i += 2)
            {
                oddReady.WaitOne();
                Console.WriteLine(i);
                evenReady.Set();
            }
        }
    }
}

results matching ""

    No results matching ""