Introduction and usage scenarios AutoResetEvent

AutoResetEvent  allowing threads to communicate with each other via signaling. Typically, this involves communication thread needs exclusive access to resources.

Thread by calling  AutoResetEvent  WaitOne on the waiting signal. If  AutoResetEvent  in a non-termination state, the thread is blocked, and threads that are currently waiting for control of resources
signaled resource available by calling Set.

Calls  Set  to  AutoResetEvent  signal to release the waiting thread. AutoResetEvent  will remain terminated until a thread is waiting to be released, and then automatically return to a non-termination status. If no threads are waiting, the status will remain indefinitely terminated state.

It can be passed to the constructor a Boolean function to control  AutoResetEvent  initial state, if the initial state is the final state, compared  to true ; otherwise  to false .

Popular terms only, etc. myResetEven.Set () after a successful run, myResetEven.WaitOne () to be able to get a chance to run; Set is signaled, WaitOne is waiting for the signal, only made a signal,
will perform the wait. If you do not send it, back WaitOne program will never be executed. Let's give an example: I went to the bookstore to buy books, when I checked a book I will go to pay the cashier,
pay good money to go to the warehouse and then pick up the book. This order can not be reversed, I, as the main thread, the cashier and warehouse worker thread to do two, as follows:

 1 using System;
 2 using System.Linq;
 3 using System.Activities;
 4 using System.Activities.Statements;
 5 using System.Threading;
 6 
 7 namespace CaryAREDemo
 8 {
 9     class Me
10     {
11         const int numIterations = 550;
12         static AutoResetEvent myResetEvent = new AutoResetEvent(false);
13         static AutoResetEvent ChangeEvent = new AutoResetEvent(false);
14         //static ManualResetEvent myResetEvent = new ManualResetEvent(false);
15         //static ManualResetEvent ChangeEvent = new ManualResetEvent(false);
16         static int number; //这是关键资源
17 
18         static void Main()
19         {
20             Thread payMoneyThread = new Thread(new ThreadStart(PayMoneyProc));
21             payMoneyThread.Name = "付钱线程";
22             = GetBookThread the Thread new new the Thread ( new new the ThreadStart (GetBookProc));
 23 is              getBookThread.Name = " take book thread " ;
 24              payMoneyThread.Start ();
 25              getBookThread.Start ();
 26 is  
27              for ( int I = . 1 ; I <= numIterations; I ++ )
 28              {
 29                  Console.WriteLine ( " books threads: number 0} { " , I);
 30                  number = I;
 31 is                  //Signal that a value has been written.
32                 myResetEvent.Set();
33                 ChangeEvent.Set();
34                 Thread.Sleep(0);
35             }
36             payMoneyThread.Abort();
37             getBookThread.Abort();
38         }
39 
40         static void PayMoneyProc()
41         {
42             while (true)
43             {
44                 myResetEvent.WaitOne();
45                 //myResetEvent.Reset();
46                 Console.WriteLine("{0}:数量{1}", Thread.CurrentThread.Name, number);
47             }
48         }
49         static void GetBookProc()
50         {
51             while (true)
52             {
53                 ChangeEvent.WaitOne();
54                 // ChangeEvent.Reset();               
55                 Console.WriteLine("{0}:数量{1}", Thread.CurrentThread.Name, number);
56                 Console.WriteLine("------------------------------------------");
57                 Thread.Sleep(0);
58             }
59         }
60     }
61 }

 

Guess you like

Origin www.cnblogs.com/fanfan-90/p/12006822.html