WaitHandle, AutoResetEvent, and ManualResetEvent

Source: Internet
Author: User

 

WaitHandleIs a class used to control thread synchronization, with the WaitOne () method, used to stop the current thread (in advance its status is Nonsignaled, that is, the red light, which will be explained later ), until the current WaitHandle receives the green light signal. WaitHandle has two statuses: "Signaled" and "Nonsignaled". The former can be understood as a green light, while the WaitOne method is invalid when the green light is displayed. The current thread is not blocked, and the latter can be considered as a red light, the WaitOne () method blocks the current thread. These two States are Set through the functions Set () and Reset. The former is set to the green light, and the latter is set to the red light.
(1): In general, the initial state is "Nonsignaled", that is, the red light. In this way, the thread will pause when WaitOne is called, which is what we usually want to do when WaitOne is called. This is exactly why Reset (literally "Reset") is defined as a red light. Therefore, when initializing, use false to indicate that the initial status is a red light. ManualResetEvent manualWait = new ManualResetEvent (false); (2): WaitHandle does not have the Set method, so the paused thread cannot be continued. Therefore, WaitHandle is generally used. It is often used as a parameter, especially when AutoResetEvent and ManualResetEvent are used, providing a certain degree of universality.
  AutoResetEvent , ManualResetEventBoth are inherited from WaitHandle and both have the Set () and Reset () methods. Of course, they also inherit the WaitOne () methods and two states. (1) The Set () and Reset () Methods of ManualResetEvent are the above functions. The statuses are Set to green and red respectively. (2) AutoResetEvent is special. As shown in the name, after Set () is complete, it will automatically call the Reset immediately, so generally only use the Set () method.
Note: This type of object can be used in multiple concurrent threads, as long asWaitOne.ManualResetEventOfSetSo that all these threads can continue to run,ResetStopped again, andAutoResetEventThen passSetOnly one blocked thread can be released at a time.

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.