Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement manual thread notification in C #

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article, the editor introduces in detail "how to implement C# manual thread notification". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to implement C# manual thread notification" can help you solve your puzzles. the following is followed by the editor's ideas slowly in-depth, together to learn new knowledge.

Differences and examples

AutoResetEvent and ManualResetEvent are very similar. The difference between the two is that the former is Auto and the latter is Manua.

You can run the following example and then test the difference between the two.

AutoResetEvent example:

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"); resetEvent.WaitOne () Console.WriteLine ("② waiting, please signal me to run"); resetEvent.WaitOne (); Console.WriteLine ("③ waiting, please signal me to run"); / /. Console.WriteLine ("thread ends");}}

Example of the ManualResetEvent class:

Class Program {private static ManualResetEvent resetEvent = new ManualResetEvent (false); static void Main (string [] args) {new Thread (DoOne) .Start (); / / used to constantly signal while (true) {Console.ReadKey (); resetEvent.Set () to another thread / / Notification occurs, set termination status}} public static void DoOne () {Console.WriteLine ("waiting, please signal me to run"); resetEvent.WaitOne (); / / none of the following is valid, and the thread will skip directly without waiting for resetEvent.WaitOne () ResetEvent.WaitOne (); Console.WriteLine (Thread end);}}

Because the AutoResetEvent object automatically resets to the non-terminating state after waiting for the signal in the .WaitOne () method, which is equivalent to the automatic gate of the high-speed toll station, and the machine shuts off automatically after a car passes by.

ManualResetEvent is equivalent to an artificial gate. If it is opened, it will be closed manually, otherwise the gate will remain open all the time.

ManualResetEvent is mainly used for more flexible thread signaling scenarios.

ManualResetEvent class

Represents a thread synchronization event that must be manually reset if it is to take effect next time when a signal is received.

Because the ManualResetEvent class is very close to the AutoManualResetEvent class, I won't go into it here.

The main differences in their use are:

The AutoResetEvent class, Set () at a time, skips one WaitOne (). Because the settings are automatically restored, you will continue to wait the next time you encounter WaitOne ().

The ManualResetEvent class, after Set (), does not reset the setting, so once Set () is used, it will go all the way and no longer wait.

Its constructor is as follows:

The constructor indicates that ManualResetEvent (Boolean) initializes a new instance of the ManualResetEvent class with a Boolean value indicating whether the initial state is set to terminate.

The common methods are as follows:

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. ManualResetEventSlim

Compared to ManualResetEvent, ManualResetEventSlim can be used to achieve better performance than ManualResetEvent when the wait time is expected to be very short and the event does not cross process boundaries.

From the point of view of code use, there is no difference, mainly when considering performance, the two different scenarios.

I will not repeat these two types here.

After reading this, the article "how to implement manual thread notification in C#" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report