AutoResetEvent and ManualResetEvent

Source: Internet
Author: User

In C # multi-threaded programming, these two classes are almost indispensable. Their usage/Declaration are similar. So what is the difference?

Set Method sets the signal to the sending status Reset method sets the signal to the non-sending status WaitOne waiting for the signal to be sent

In fact, we can see from the name that the manual and automatic methods actually refer to the Reset method processing, as shown in the following example.

Public AutoResetEvent autoevent = new AutoResetEvent (true );

Public ManualResetEvent manualevent = new ManualResetEvent (true );

All signals are sent by default,

Autoevent. WaitOne ();

Manualevent. WaitOne ();

If a thread calls the above method, when the signal is in the sending status, the thread will receive a signal to continue execution.

After the difference is called, autoevent. WaitOne () allows only one thread to enter each time. When a thread gets a signal (that is, other threads call

Autoevent. after the Set () method), autoevent automatically sets the signal to not sent, and other threads that call WaitOne will only wait. that is to say, autoevent only wakes up one thread at a time

Manualevent can wake up multiple threads, because when a thread calls the set method, other threads that call waitone receive signals to continue execution, manualevent does not automatically set the signal to not send. that is, unless manualevent is manually called. reset (). method, then

Manualevent will remain in a signal state, and manualevent will be able to wake up multiple threads at the same time to continue execution

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.