AutoResetEvent and ManualResetEvent comprehension, autoresetevent

Source: Internet
Author: User

AutoResetEvent and ManualResetEvent comprehension, autoresetevent

AutoResetEvent and ManualResetEvent are used to control the code execution sequence between multiple threads. They inherit from WaitHandle and have the same API, but they are different in use.

Although I understood it every time, I forgot it in a few days because I didn't think deeply about my memory. We need to re-understand these two classes when we need to use them next time.

I met them again today and wrote this essay .. Deepen understanding. You don't have to worry about them in the future.

1. constructor Parameters

Var manualResetEventWaitHandle = new ManualResetEvent (true );

Var autoResetEventResetEventWaitHandle = new AutoResetEvent (true );

MSDN explanation: True has a signal, and False does not.

True. When WaitHandle. WaitOne () is encountered during code execution, the (with a signal) thread will not be blocked and will continue to be executed.

If it is False, WaitHandle is encountered. when WaitOne (), the current thread is suspended (no signal, wait for the signal) until another thread executes WaitHandle. set (), the thread will continue to execute WaitHandle. code after WaitOne

 

2. Set and Reset Methods

MSDN explanation: Set is the terminated state (with signal), and Reset is Set to the non-terminated state (with no signal)

It is better to understand the parameters of constructor.

Var waitHandle = new ManualResetEvent/AutoResetEvent (false)

WaitHandle. Set ()

Equivalent

Var waitHandle = new ManualResetEvent/AutoResetEvent (true)

Likewise

Var waitHandle = new ManualResetEvent/AutoResetEvent (true)

WaitHandle. Reset ()

Equivalent

Var waitHandle = new ManualResetEvent/AutoResetEvent (false)

 

3. AutoResetEvent and ManualResetEvent

It's the turn of the main character. In comparison to the subway gate, when the gate is enabled (with no signal), a passenger cannot pass (WaitOne) before arriving at the gate. After swiping the card (Set), the gate is closed (with a signal) and the passenger enters. Then the gate is enabled again (no signal ),

Highlighting is the difference between them. It can be imagined that the gate is an AutoResetEvent. When the gate is opened again after a guest passes through the gate, the gate is still closed after the first person swipe the card when the gate encounters a fault, in this way, passengers can continue to enter without WaitOne.

That is

AutoResetEventResetEventWaitHandle. Set ()

Equivalent

ManualResetEventWaitHandle. Set ()

ManualResetEventWaitHandle. Reset ();

 

Supplement ManualResetEventSlim

ManualResetEvent lightweight and optimized version in Framework4, which is recommended

 

Refer:

Http://www.cnblogs.com/qingyun163/archive/2013/01/05/2846633.html

Http://baike.baidu.com/link? Url = Cu-Oakr8hkaYtpg9hPTMuuUtAiMdSBc-f_LCs3-dtWTcYSIvbl9b22AKi63MyZ3I9RtmYtUCnYtVFsjTrQiNVq

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.