This article mainly for you to introduce in detail the C # use AutoResetEvent to achieve synchronization of relevant data, with a certain reference value, interested in small partners can refer to
The problem with the sequential execution of a thread a few days ago is that an asynchronous thread sends a data request to the A interface. Another asynchronous thread sends a data request to the B interface, and when both A and B are successful, a request is sent to the C interface. Seriously, the BS project has been done, and it's not much to know about threading. I know AutoResetEvent this thing is related to thread, used to handle thread switching and so on, so decided to use AutoResetEvent to deal with the above problem.
Then find the relevant information online:
It turns out that AutoResetEvent is often used in. NET multithreaded programming. When a thread calls the WaitOne method, the signal is sent, the thread gets a signal, and the program continues to execute down, otherwise it waits. and Autoresetevent.waitone () only allows one thread at a time, and when a thread gets a signal, AutoResetEvent automatically and sends the signal to the non-sending State, and the other thread that calls WaitOne waits. That is to say, AutoResetEvent wakes only one thread at a time, and the other threads are still blocked.
Brief introduction
AutoResetEvent (bool initialstate): A constructor that initializes a new instance of the class with a Boolean value that indicates whether the initial state is set to signaled.
False: No signal, the WaitOne method of the child thread is not automatically called
True: There is a signal that the WaitOne method of the child thread is automatically called
Reset (): Sets the event state to a non-terminating state, causes the thread to block, or returns true if the operation succeeds; otherwise, returns false.
Set (): Sets the event state to the signaled state, allows one or more waiting threads to continue, or returns true if the operation succeeds; otherwise, returns false.
WaitOne (): Blocks the current thread until a signal is received.
WaitOne (TimeSpan, Boolean): Blocks the current thread until the current instance receives a signal, uses a TimeSpan to measure the interval, and specifies whether to exit the synchronization domain before waiting.
WaitAll (): Wait for all signals.
Realize
Class Program {static void Main () {Request Req = new request (); This man is going to do three things. Thread getcarthread = new Thread (new ThreadStart (req). Interfacea)); Getcarthread.start (); Thread gethousethead = new Thread (new ThreadStart (req. INTERFACEB)); Gethousethead.start (); Waiting for three things to do the good news Notice information Autoresetevent.waitall (req.autoevents); The man was happy. Req. INTERFACEC (); System.Console.ReadKey (); }} public class Request {//build Event array public autoresetevent[] autoevents = null; Public Request () {autoevents = new autoresetevent[] {new AutoResetEvent (false), new AutoResetEvent (FALSE)} ; } public void Interfacea () {System.Console.WriteLine ("Request a Interface"); Thread.Sleep (1000*2); Autoevents[0]. Set (); System.Console.WriteLine ("A Interface Complete"); } public void Interfaceb () {System.Console.WriteLine ("Request B Interface"); Thread.Sleep (1000 * 1); AUTOEVENTS[1]. Set (); System.Console.WriteLine ("B Interface Complete"); } public void Interfacec () {System.Console.WriteLine ("Two interfaces have been requested, processing C"); } }
Note that WaitOne or WaitAll are best added to the time-out period. Otherwise, the thread is blocked until the signal is received.
Something
This is just a simplification of the scene above, mainly to solve the problem of the scene that I just said.
These are my own summary of the use of AutoResetEvent. The shortcomings, please point twos.