Manualresetevent and autoresetevent can be used to control the running of threads and communication between threads. Msdn reference:
Http://msdn.microsoft.com/zh-cn/library/system.threading.autoresetevent.aspx
Http://msdn.microsoft.com/zh-cn/library/system.threading.manualresetevent.aspx
I will write an example below. Here we simulate a thread to update data and two threads to read data. During the update process, you need to block the read operations. There is also a semaphore to control the exit of the thread.
Using system; using system. collections. generic; using system. componentmodel; using system. data; using system. drawing; using system. text; using system. windows. forms; namespace windowsapplication35 {public partial class form1: FORM {public form1 () {initializecomponent ();} system. threading. manualresetevent mevent = new system. threading. manualresetevent (true); // determine the semaphore system for thread security exit. threading. manualresetev Ent meventstopall = new system. Threading. manualresetevent (false); // use ******* manualresetevent. Private void button#click (Object sender, eventargs e) {// a thread simulates writing to new system. threading. thread (invokewrite ). start (); // two threads simulate reading new system. threading. thread (invokeread ). start (); new system. threading. thread (invokeread ). start ();} private void invokewrite () {for (INT I = 0; I <100; I ++) {// determine the thread to exit safely if (meventstopall. waitone (10, false) = true) break; // set the semaphore. Assume that it takes 2 seconds to update data, and every update is paused for 2 seconds. mevent. reset (); console. writeline ("Updating... "); system. threading. thread. sleep (2000); mevent. set (); system. threading. thread. sleep (2000) ;}} private void invokeread () {While (mevent. waitone () = true) {// determine whether the thread exits safely if (meventstopall. waitone (10, false) = true) break; // assume that it takes 10 milliseconds to read the whole data. he needs to determine the semaphore switch. console. writeline ("read a piece of data:"); system. threading. thread. sleep (10) ;}} private void form=formclosing (Object sender, formclosingeventargs e) {meventstopall. set ();}}}