C # thread-based thread control

Source: Internet
Author: User

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 ();}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.