This 2 is a signaling methodology. E.g. you have 2 thread , Thread 2 can call Thread 1 stop in middle, and call thread 1 continue run again later.
AutoResetEvent
Only allow 1 Thread to run, if u have 3 WaitOne(), you need to have 3 Set().
ManualResetEvent
Allow all Thread to run, if u have 3 WaitOne(), you only need to have 1 Set().
// static AutoResetEvent objAuto = new AutoResetEvent(false);
static ManualResetEvent objAuto = new ManualResetEvent(false);
static void Main(string[] args)
{
new Thread(SomeMethod).Start();
Console.WriteLine("Main");
Console.ReadLine();
objAuto.Set();
Console.ReadLine();
objAuto.Set();
Console.ReadLine();
}
static void SomeMethod()
{
Console.WriteLine("Start 1");
objAuto.WaitOne();
Console.WriteLine("Finish 1");
Console.WriteLine("Start 2");
objAuto.WaitOne();
Console.WriteLine("Finish 2");
}
Result (Auto)
------
Main
Start 1
(Enter)
Finish 1
Start 2
(Enter)
Finish 2
Result (Manual)
------
Main
Start 1
(Enter)
Finish 1
Start 2
Finish 2
Result (Auto)
------
Main
Start 1
(Enter)
Finish 1
Start 2
(Enter)
Finish 2
Result (Manual)
------
Main
Start 1
(Enter)
Finish 1
Start 2
Finish 2
No comments:
Post a Comment