This article is a mirror article of machine translation, please click here to jump to the original article.

View: 24412|Reply: 1

[Source] The difference between ManualResetEvent and AutoResetEvent learned by C# threads

[Copy link]
Posted on 5/31/2018 2:59:47 PM | | | |
Let's discuss ManualResetEvent first, and I will intersperse some AutoResetEvent content during the discussion to compare:

ManualResetEvent can block one or more threads until a signal is received telling ManualResetEvent to stop blocking the current thread.

Information about ManualResetEvent

.net/c# Thread thread Suspend and Resume obsolete solutions
https://www.itsvse.com/thread-4769-1-1.html
(Source: Architect_Programmer)


Imagine that the ManualResetEvent object has a Boolean type property, IsRelease, to control whether to block the current thread. We can set this property when initializing, such as ManualResetEvent event=new ManualResetEvent(false); This indicates that the default property is to block the current thread.

Code example:



Note an important difference between ManualResetEvent and AutoResetEvent here:

manual will definitely send a signal to both thread 1 and thread 2,Auto will only send a signal to one of them randomly.

Why is one called manual and the other called auto? I think this is a question that many people have, and now we will look at this question.

Just now_manualResetEvent . Set(); I think everyone understands this sentence, which can be regarded as setting the property of IsRelease to true

_manualResetEvent.WaitOne(); No longer blocks thread 1 after receiving a signal. The value of IsRelease is true throughout the entire process after that.If

If you want to return the value of IsRelease to false, you must call the _manualResetEvent.Reset() method again.

If it is _autoResetEvent.set(), then _autoResetEvent.WaitOne() will automatically set the value of IsRelease to false.

That's why one is called auto and the other is manual.



Test code:







Previous:. .NET's fastest OOM object mapping component, Tiny Mapper, in project practice
Next:C# DEBUG debug information printing and output detailed explanation
 Landlord| Posted on 12/18/2024 8:30:44 PM |
Both ManualResetEvent and AutoResetEvent inherit from EventWaitHandle, as shown in the figure below:



Reference:The hyperlink login is visible.

Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com