[Test] public void reseteventtest () {var alldone = new manualresetevent (false); var I = 0; console. writeline ("starts the loop. "); Threadpool. queueuserworkitem (x) =>{ while (I <5) {I ++; thread. sleep (1, 1000); console. writeline ("loop [{0}] Sleep ends", I);} alldone. set (); // end loop, which can be executed later from waitone}); console. writeline ("start waiting"); alldone. waitone (); console. writeline ("you have received the set notification and can proceed later. ");}
The above is not much explained. It is much more useful to look at the execution results by yourself.
Sometimes we need to wait for the asynchronous (or trace event) returned results in the synchronous return method, in addition to loop wait, we can also achieve this:
Public virtual ActionResultWrap Request <T> (string region name, T param) {allDone. reset (); // Reset. The following methods are waiting for returning. // var hasReturn = false; var wrap = new ActionResultWrap (); ReceiveWrap = (x) =>{ wrap = x; // hasReturn = true; allDone. set () ;}; var swrap = new DatagramWrap (); Send (swrap. toBson (partition name, param); allDone. waitOne (); // This is the method for loop wait // while (! HasReturn) // {// Thread. Sleep (20); //} return wrap ;}