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