In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of how to implement thread notification in C#. The content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this article on how to implement thread notification in C#. Let's take a look.
The AutoRestEvent class is used to send notifications from one thread to another.
The Microsoft documentation states that thread synchronization events are automatically reset when a waiting thread is released and receives a signal.
There is only one constructor:
The parameters in the constructor are used to set the signal state.
The constructor indicates that AutoResetEvent (Boolean) initializes a new instance of the AutoResetEvent class with a Boolean value indicating whether the initial state is set to terminate.
What a terrible machine translation.
Common methods
What does the AutoRestEvent class do and what are the parameters of the constructor? Don't worry, let's take a look at the common methods of this class:
The method states that Close () releases all resources occupied by the current WaitHandle. Reset () sets the event state to non-terminating, resulting in thread blocking. Set () sets the event state to signaled, allowing one or more waiting threads to continue execution. WaitOne () blocks the current thread until the current WaitHandle receives a signal. WaitOne (Int32) blocks the current thread until the current WaitHandle receives the signal, while specifying the time interval in milliseconds using a 32-bit signed integer. WaitOne (Int32, Boolean) blocks the current thread until the current WaitHandle receives the signal, using a 32-bit signed integer to specify the time interval and whether to exit the synchronization domain before waiting. WaitOne (TimeSpan) blocks the current thread until the current instance receives a signal and uses TimeSpan to specify the time interval. WaitOne (TimeSpan, Boolean) blocks the current thread until the current instance receives the signal, using TimeSpan to specify the time interval and whether to exit the synchronization domain before waiting. A simple example
Here we write a program like this:
Create a thread that can perform tasks in multiple stages; each phase completed, you need to stop and wait for the child thread to be notified before you can proceed to the next step.
.WaitOne () is used to wait for another thread to send a notification
.Set () is used to notify the thread, and the AutoResetEvent becomes terminated.
.reset () is used to reset the AutoResetEvent state
Class Program {/ / Thread Notification private static AutoResetEvent resetEvent = new AutoResetEvent (false); static void Main (string [] args) {/ / create thread new Thread (DoOne). Start (); / / used to constantly signal while (true) {Console.ReadKey () to another thread ResetEvent.Set (); / / Notification occurs, setting termination status}} public static void DoOne () {Console.WriteLine ("waiting, please signal me to run"); / / wait for other threads to send signal resetEvent.WaitOne () Console.WriteLine ("\ nreceive the signal, continue to execute"); for (int I = 0; I < 5; iTunes +) Thread.Sleep (TimeSpan.FromSeconds (0.5)); resetEvent.Reset (); / / reset to non-terminating Console.WriteLine ("\ nThe first phase is finished, please continue to give instructions") / / wait for other threads to send a signal resetEvent.WaitOne (); Console.WriteLine ("\ nreceive the signal, continue to execute"); for (int I = 0; I < 5; iSuppli +) Thread.Sleep (TimeSpan.FromSeconds (0.5)); Console.WriteLine ("\ nThe second phase is finished and the thread ends, please close the window manually") }} explain.
AutoResetEvent objects have terminating and non-terminating states. Set () sets the termination state, and Reset () resets the non-termination state.
This termination state can be understood as that the signal has been notified, while the non-termination state means that the signal has not been notified.
Note that terminating state and non-terminating state refer to the state of the AutoResetEvent, not the state of the thread.
The thread waits for the signal by calling the WaitOne () method
Another thread can call Set () to tell AutoResetEvent to release the waiting thread.
Then the AutoResetEvent becomes terminated.
It is important to note that if the AutoResetEvent is already in a terminated state, the thread call WaitOne () will no longer work. Unless Reset () is called.
The parameters in the constructor set this state. True represents the termination status, and false represents the non-termination status. If you use new AutoResetEvent (true);, the thread does not have to wait for the signal at first.
After using the type, you should release the type directly or indirectly, explicitly call Close () / Dispose (), or use using. Of course, you can also exit the program directly.
It is important to note that if the interval between multiple calls to Set () is too short, and if the first Set () is not finished (the signal transmission takes processing time), then the second Set () may not be valid (it will not work).
A more complex example
We design a program:
The Two thread starts to block.
Thread One can set thread Two to continue running and then block itself
Thread Two can set One to continue running and then block itself
The program code is as follows (after running, please set the keyboard to English input state and press the key):
Class Program {/ / Control the first thread / / when the first thread starts, AutoResetEvent is terminated without waiting for the signal private static AutoResetEvent oneResetEvent = new AutoResetEvent (true); / / controls the second thread / / when the second thread starts, AutoResetEvent is in a non-terminating state and needs to wait for the signal private static AutoResetEvent twoResetEvent = new AutoResetEvent (false) Static void Main (string [] args) {new Thread (DoOne). Start (); new Thread (DoTwo). Start (); Console.ReadKey ();} public static void DoOne () {while (true) {Console.WriteLine ("\ n ① press the key and I'll let DoTwo run") Console.ReadKey (); twoResetEvent.Set (); oneResetEvent.Reset (); / / wait for DoTwo () to signal me oneResetEvent.WaitOne (); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine ("\ n DoOne () execute") Console.ForegroundColor = ConsoleColor.White;}} public static void DoTwo () {while (true) {Thread.Sleep (TimeSpan.FromSeconds (1)); / / wait for DoOne () to signal me twoResetEvent.WaitOne (); Console.ForegroundColor = ConsoleColor.Yellow Console.WriteLine ("\ nDoTwo () execute"); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine ("\ n ② press the key and I'll let DoOne run"); Console.ReadKey (); oneResetEvent.Set (); twoResetEvent.Reset ();}
explain
Two threads have the function of blocking themselves and unblocking the other thread.
Understand it with a picture in the movie Best partner.
DoOne and DoTwo take turns breathing. They can't control their own breathing, but they can decide others' breathing.
You screw me, I screw you, and you can breathe.
Of course, WaitOne () can also set the waiting time. If the bald guy (DoOne) does not let DoTwo breathe, after waiting for a certain period of time, he can forcibly shake the balance and fall to the ground to breathe.
Note that AutoRestEvent is prone to deadlocks if it is not used properly.
In addition, AutoRestEvent uses kernel time mode, so the wait time should not be too long, otherwise it will take more CPU time.
AutoResetEvent is also suitable for thread synchronization.
In addition, after WaitOne () is used in a thread and another thread uses Set () notification, the AutoResetEvent object automatically returns to a non-terminating state without requiring the thread to use Reset ().
This is the end of the article on "how to implement Thread Notification in C#". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to implement thread notification in C#". If you want to learn more knowledge, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.