A few days ago, I encountered a problem of sequential execution of a thread, which was that an asynchronous thread sent a data request to interface A. Another asynchronous thread sends a data request to interface B. When both A and B execute successfully, it sends a request to interface C. To be honest, I have been working on BS projects and don’t know much about threads. I knew that AutoResetEvent is related to threads and is used to deal with thread switching, so I decided to use AutoResetEvent to deal with the above problems.
So search for relevant information online:
It turns out that AutoResetEvent is often used in .Net multi-threaded programming. When a thread calls the WaitOne method, the signal is in the sending state, the thread will get the signal, and the program will continue to execute downward, otherwise it will wait. Moreover () Only one thread is allowed to enter at a time. When a thread gets the signal, AutoResetEvent will automatically set the signal to the non-send state. Other threads calling WaitOne can only continue to wait. In other words, AutoResetEvent only wakes up one thread at a time, and other threads are still blocked.
Introduction
AutoResetEvent(bool initialState): Constructor, initializes a new instance of the class with a boolean value indicating whether to set the initial state to terminate.
false: No signal, the WaitOne method of the child thread will not be automatically called
true: There is a signal, the WaitOne method of the child thread will be automatically called
Reset (): Set the event status to a non-terminated state, causing thread blockage; if the operation is successful, return true; otherwise, return false.
Set (): Set the event status to the termination status, allowing one or more waiting threads to continue; if the operation is successful, return true; otherwise, return 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 TimeSpan to measure the time interval and specifies whether to exit the synchronization domain before waiting.
WaitAll(): Wait for all signals.
accomplish
class Program { static void Main() { Request req = new Request(); //This person does three big things Thread GetCarThread = new Thread(new ThreadStart()); (); Thread GetHouseThead = new Thread(new ThreadStart()); (); //Waiting for good news notifications to complete all three things (); //This person is happy. (); (); } } public class Request { //Create an event array public AutoResetEvent[] autoEvents = null; public Request() { autoEvents = new AutoResetEvent[] { new AutoResetEvent(false), new AutoResetEvent(false) }; } public void InterfaceA() { ("Request Interface A"); (1000*2); autoEvents[0].Set(); ("A interface completed"); } public void InterfaceB() { ("Request B interface"); (1000 * 1); autoEvents[1].Set(); ("B interface completed"); } public void InterfaceC() { ("Both interfaces have been requested and are processing C"); } }
Note that it is best to add timeout for WaitOne or WaitAll. Otherwise, no signal is received and the thread will be blocked.
Later stories
This is just a simplification of the above scenario, mainly used to solve the problem of the scenario I just mentioned.
The above is a summary of your use of AutoResetEvent. Please give me some advice on the shortcomings.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.