First, ManualResetEvent class
Used to keep a thread in a wait state. There are two common methods:
Set (): Sets the status to signaled.
Restset (): Resets the status to no signal.
WaitOne: Waits for an event object to become signaled.
[STAThread] Public Static voidMain () {ManualResetEvent manre; Manre=NewManualResetEvent (true);//the initial state is a signal BOOLState = Manre.waitone ( the,true);//in a signaled state, even if the wait time is set, the thread will not wait within the WaitOne methodConsole.WriteLine ("ManualResetEvent after first WaitOne--"+ state);//True//status changed to No signalManre.reset (); State= Manre.waitone ( the,true);//no signal state, the thread waits for a specified length of timeConsole.WriteLine ("ManualResetEvent after second WaitOne--"+ state);//FalseConsole.ReadLine (); }
The ManualResetEvent class also has WaitAny, WaitAll, and the usage can be inferred from the literal meaning.
Second, Autoresetenvet class
Usage is similar to the ManualResetEvent class, with the only difference being in the WaitOne method. After the WaitOne method is executed, the signal state is automatically transformed.
If the example in the previous example is implemented with Autoresetenvet, the following:
1 Public Static voidMain ()2 {3 AutoResetEvent is;4is =NewAutoResetEvent (true);//Initial termination State5Console.WriteLine ("before first WaitOne");6 BOOLState = Are.waitone ( the,true);//does not wait (because the initial state is signaled), then the signal automatically becomes false. 7Console.WriteLine ("After first WaitOne--"+State );8 //status changed to No signal9 //Are.reset ();//you no longer need to manually change the signal status. TenState = Are.waitone ( the,true); OneConsole.WriteLine ("After second WaitOne--"+State ); A console.readline (); -}
Third, the Mutex class
Thread synchronization----Manual synchronization