C # thread Communication one

Source: Internet
Author: User

C # multithreaded Communication

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading;6 usingSystem.Threading.Tasks;7 8 namespaceApp.Test.Csharep. Thread Communication9 {Ten      Public classMainthread One     { A         /// <summary> -         ///Start Execution Notification -         /// </summary> the         PrivateEventWaitHandle datachangeevent =NewAutoResetEvent (false); -          -  -          Public intOpendoornum =0; +  -         /// <summary> +         ///testing process: 10 times after the opening of the door, each door completed to complete the door operation, the main thread completed the door, the child thread completed the door A         /// </summary> at          Public voidRun () -         { -             varThread =NewThread (NewThreadStart (Inputdoor)); -Thread. Name ="Door Thread"; -Thread. IsBackground =true; - thread. Start (); in  -Thread.Sleep ( -); to  +              for(inti =0; I <Ten; i++) -             { theopendoornum++; *Console.WriteLine ("AA completed opening the first"+ Opendoornum +"Second Door"); $ Panax Notoginseng                 ///The notification has completed the open door operation - Datachangeevent.set (); the  +                 ///after Datachangeevent.set () is executed, Datachangeevent.waitone () begins to obtain a signal to prevent the signal from being acquired A                 ///executes the Datachangeevent.set () method again, the last Datachangeevent.waitone () code that needs to be executed is not executed . the                 ///so temporarily sleep, or the door may not be enough times +Thread.Sleep (1); -             } $  $ Console.read (); -         } -  the         /// <summary> -         ///Complete the door operationWuyi         /// </summary> the         Private voidInputdoor () -         { Wu             ///the thread notification of the open door is received, and the WaitOne method is always in the blocking code if it can receive a signal in time before the notification thread set () -             ///If a signal is obtained during the Datachangeevent.waitone () process and the called Thread gives set (), then Datachangeevent.waitone () About             ///The signal should not be received, so it should be dormant after executing datachangeevent.set () in the calling thread, after calling Datachangeevent.set () $  -             ///ensure that the child thread is always in a running state -              while(true) -             { A                 //if (Datachangeevent.waitone ()) +                 //{ the                 //Console.WriteLine ("\ta" + Opendoornum + "second Door"); -                 //} $  the                 ///wait 100 milliseconds, no signal is released, continue down execution, date will be assigned the                 if(Datachangeevent.waitone ( -,false)) the                 { theConsole.WriteLine ("\ t ""+ Thread.CurrentThread.Name +""a first"+ Opendoornum +"Second Door"); -                 } in             } the         } the     } About}

C # thread Communication one

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.