This is better understood.
As the book says, as the house that has been opening the door, who will go in, who will close their doors, when out of the open.
There are three common methods:
Set ()//sets to have a signal, that is, to let the waiting thread do not continue to wait, wake up the waiting thread.
Reset ()//set to no signal means that no waiting thread becomes a waiting state, and the wait method needs to be matched.
Wait ()//waits for the thread to be blocked unless the signal "set method" is accepted to release
So the example in the book:
classProgram {Static voidTest (stringThreadName,intseconds) {Console.WriteLine ($"{ThreadName} falls to sleep"); Thread.Sleep (timespan.fromseconds (seconds)); Console.WriteLine ($"{threadname} waits for the gates to open"); _mainevent.wait (); Console.WriteLine ($"{ThreadName} enters the GATS"); } StaticManualResetEventSlim _mainevent =NewManualResetEventSlim (false); Static voidMain (string[] args) { varT1 =NewThread (() = Test ("Thread 1",5)); vart2 =NewThread (() = Test ("Thread 2",6)); varT3 =NewThread (() = Test ("Thread 3", A)); T1. Start (); T2. Start (); T3. Start (); Thread.Sleep (Timespan.fromseconds (6)); Console.WriteLine ("The gates is now open"); _mainevent.set (); Thread.Sleep (Timespan.fromseconds (2)); _mainevent.reset (); Console.WriteLine ("The Gates has been closed!"); Thread.Sleep (Timespan.fromseconds (Ten)); Console.WriteLine ("The fatres is now Ioen for the second time!"); _mainevent.set (); Thread.Sleep (Timespan.fromseconds (2)); Console.WriteLine ("The Gates has been closed"); _mainevent.reset (); Console.readkey (); } }
Run, and then match the interpretation on the book.
It's a good idea to think about it a little bit.
is to do something, it will be more trouble, but every step of the thread will be fully mastered.
When initialized to False
The general sequence of operations is
Reset ()//settings need to wait
Wait ()//actual waiting
Set ()//release thread
In general, initialization of ManualResetEventSlim is false.
If true, reset must be set manually once.
"C # Multithreaded Programming combat" 2.6 ManualResetEventSlim