|
1
beginner
0
intermediate
0
advanced
|
|||
|
||||
|
ManualResetEvent and AutoResetEvents are signalling constructs. We'd use these constructs if we want threads to wait until signalled. ManualResetEvent is like a door. If the door is closed all threads would wait at the door. Some other thread can signal for the door to open. Once opened all threads can pass through the "door" and continue execution. The door can be closed again. Creating the ManualResetEvent object. (the "door") ManualResetEvent mre = new ManualResetEvent(false); false = door is closed by default, true = door is open by default. Wait for the door to open: mre.WaitOne(); Opening the door: mre.Set(); Closing the door: mre.Reset(); AutoResetEvent is similar to ManualResetEvent except that it behaves as a ticket turnstile instead of a door i.e. when signalled only one thread is allowed to proceed and the gate is automatically closed. Other threads then have to wait for more signals. Creating a AutoResetEvent. AutoResetEvent are = new AutoResetEvent(false); false = turnstile is created as closed , true = turnstile is created as open. Other methods are similar to ManualResetEvent. Note that if no thread is waiting and a Set is called, the signal is not lost. The "turnstile" remains open until some thread calls WaitOne and is allowed to enter. |
|||
|
||||