MANUALREASETEVENT VS AUTORESETEVENT
MANUALREASETEVENT VS AUTORESETEVENT
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ManualResetEvent_
{
class Program
{
static ConsoleColor prevColor = Console.ForegroundColor;
static AutoResetEvent autoreset = new AutoResetEvent(false); //declaring Autoreset event and markes it as static so all class can share it
static ManualResetEvent manualreset = new ManualResetEvent(false); //declaring Manualreset event and markes it as static so all class can share it
static void Main(string[] args)
{
Console.Title = "AutoResetEvent and ManualResetEvent Example";
Thread t1 = new Thread(new ThreadStart(AutoResetMethod)); //AutoResetEvent is the method which will start when the thread starts
Thread t2 = new Thread(new ThreadStart(ManualResetMethod)); //ManualResetEvent is the method which will start when the thread starts
t1.Name = "Thread1";
t2.Name = "Thread2";
Thread.CurrentThread.Name = "Main Thread";
//t1.Start();
t2.Start();
Console.ReadLine();
Console.WriteLine("Thread1 is paused for the main t1");
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("\t\t\t Running the Thread {0}", Thread.CurrentThread.Name);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Executing task of the Main Thread");
for (int i = 0; i < 5; i++)
{
Console.Write("{0}", i);
Thread.Sleep(1000);
}
Console.WriteLine();
Console.WriteLine("Task Completed on Main Thread");
manualreset.Set();
// autoreset.Set();
Console.ReadLine();
Console.ForegroundColor=ConsoleColor.Magenta;
Console.WriteLine("\t\t\t Running Thread :{0}",Thread.CurrentThread.Name);
Console.ForegroundColor=ConsoleColor.Green;
Console.WriteLine("Thread1 Paused for task2");
Console.WriteLine("Doing some work in Main Thread......");
manualreset.Set(); // Please Uncomment this while using ManualResetEvent.
//autoreset.Set(); // Please Uncomment this while using AutoResetEvent.
Console.ForegroundColor = prevColor;
}
static void AutoResetMethod()
{
Console.ForegroundColor=ConsoleColor.Cyan;
Console.WriteLine("\t\t************** AutoResetEvent *****************\t\t");
Console.ForegroundColor=ConsoleColor.Magenta;
Console.WriteLine("\t\t\t Running the Thread {0} ", Thread.CurrentThread.Name);
Console.ForegroundColor=ConsoleColor.Yellow;
Console.WriteLine("Executing the Task");
Console.WriteLine("Press Enter to Pause task");
autoreset.WaitOne();
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("\t\t\tRunning Thread: {0}", Thread.CurrentThread.Name);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("\n===================================");
Console.WriteLine("Executing Task1.....");
for (int i = 0; i < 5; i++)
{
Console.WriteLine("Task1 executing.....");
Thread.Sleep(1000);
}
Console.WriteLine("Execution of Task1 completed..");
Console.WriteLine("===================================");
Console.WriteLine();
Console.WriteLine("===================================");
Console.WriteLine("Executing Task2......");
Console.WriteLine("Press Enter to Pause Task2..");
//Wait for second task:
autoreset.WaitOne();
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("\t\t\tRunning Thread: {0}", Thread.CurrentThread.Name);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Execution of Task2 completed...");
Console.WriteLine("===================================");
Console.ForegroundColor = prevColor;
}
static void ManualResetMethod()
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("\t\t***************** ManualResetEvent ***************");
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("\t\t\tRunning Thread: {0}", Thread.CurrentThread.Name);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Executing Task1......");
Console.WriteLine("Press Enter to Pause Task1..");
manualreset.WaitOne();
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("\t\t\tRunning Thread: {0}", Thread.CurrentThread.Name);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("\n===================================");
Console.WriteLine("Executing Task1.....");
for (int i = 0; i < 5; i++)
{
Console.WriteLine("Task1 executing.....");
Thread.Sleep(1000);
}
Console.WriteLine("Execution of Task1 completed..");
Console.WriteLine("===================================");
Console.WriteLine();
Console.WriteLine("===================================");
Console.WriteLine("Executing Task2......");
Console.WriteLine("Press Enter to Pause Task2..");
//Wait for second task:
manualreset.WaitOne();
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("\t\t\tRunning Thread: {0}", Thread.CurrentThread.Name);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Execution of Task2 completed...");
Console.WriteLine("===================================");
Console.ForegroundColor = prevColor;
}
}
}
When we use AutoResetEvent, for every WaitOne(), we must call Set(). But when we use ManualResetEvent() 1 Set() will revoke all the WaitOne() in our program.
thanks -Abhishek Yadav --http://www.c-sharpcorner.com/UploadFile/ff0d0f/autoresetevent-and-manualresetevent-in-C-Sharp/
Comments
Post a Comment