AutoResetEvent ManualResetEvent WaitOne precautions, autoreseteventreset

Source: Internet
Author: User

AutoResetEvent ManualResetEvent WaitOne precautions, autoreseteventreset

The company still had to use these old guys. They used them several times. You need to repeat the film each time you use it.

A piece is translated into a Zen Sutra. Make this thing a metaphysics. Microsoft also takes jujube pills. For more information, see @ Fengfeng lingyao's blog.

There are also some of the company's use of the wind, which is not mentioned, I leave a tip for myself. Let's take a look later. If you have any errors, I hope you can point them out. Although I may not change it.

 

AutoResetEvenT,ManualResetEvent has similar usage.

Nothing moreManualResetEvent can be notified to multiple threads and disabled by default after being enabled. You need to disable it manually.

 

AutoResetEvent a1 = new AutoResetEvent (false );

ManualResetEvent m1 = new ManualResetEvent (false );

 

If the initial value is true, it is the same as without the two items. It cannot block the current thread and waits for the notification to continue running.Therefore, it is initialized to false when used.

Therefore, initialization to true is useless. That's why we designed it like this. I did not understand the Diamond Sutra. And other helpful answers,

 

WaitOne ();

IfAutoResetEvenT is false. If there is no set (), it will be blocked all the time, that is, it will become a deadlock.

-- Vs2012 win10. net4.6.1

WaitOne (int); WaitOne (int, bool); WaitOne (timespan, bool );

The three are the same. The unit of int Is millisecond, and timespan is the time interval.

(1) IfAutoResetEvenT is false. If it is returned within the set time, the effect is the same as that of common wait (), blocking and then running;

Static AutoResetEvent a1 = new AutoResetEvent (false); static void Main (string [] args) {Thread th = new Thread (new ThreadStart (method); th. start (); Console. writeLine ("start .. "+ DateTime. now. toString ("mm: ss fff"); bool isback = a1.WaitOne (2000); // set the time Console. writeLine ("end .. "+ DateTime. now. toString ("mm: ss fff"); Console. read () ;}// do something static void method () {Thread. sleep (1000); // block one second and return to the Console within the set time. writeLine ("Async .. "+ DateTime. now. toString ("mm: ss fff"); a1.Set ();}

Other threads do not return notifications after the specified time. This thread is not blocked, just like normal waitone.

 

(2) If there is no set () within the set time, the time will not be blocked after the set time is exceeded. Return directly. It looks like an artifact. You can set the time and run a thread or Asynchronous Method. after a certain period of time, it will be returned to the aforementioned timeout.

This is often the case in development. However, if the asynchronous method or thread sets () after setting the timeout time, it can be directly run next time. Will not cause blocking. You need reset ().

If the return parameter is true, set is triggered,

If the return parameter is false, the request is triggered automatically after the specified time.

Static AutoResetEvent a1 = new AutoResetEvent (false); static void Main (string [] args) {Thread th = new Thread (new ThreadStart (method); th. start (); Console. writeLine ("start .. "+ DateTime. now. toString ("mm: ss fff"); bool isback = a1.WaitOne (2000, false); Console. writeLine ("end .. "+ DateTime. now. toString ("mm: ss fff") + isback); Console. read () ;}// do something static void method () {Thread. sleep (4000); // return to Console upon timeout. writeLine ("Async .. "+ DateTime. now. toString ("mm: ss fff"); a1.Set ();}

 

 

 

The running result indicates that it takes more than two seconds to run without blocking. The thread runs directly and runs separately. The notification is opened by other threads at this time, and the status of the notification is kept. If a program comes in next time,

Bool isback = a1.WaitOne (2000, false); in this case, it will not be blocked.
AutoResetEvent a1 = new AutoResetEvent (false); private void button#click (object sender, EventArgs e) {Thread th = new Thread (new ThreadStart (method); th. start (); listBox1.Items. add ("start .. "+ DateTime. now. toString ("mm: ss fff"); bool isback = a1.WaitOne (2000, false); listBox1.Items. add ("end .. "+ DateTime. now. toString ("mm: ss fff") + isback);} void method () {Thread. sleep (4000); // timeout returns this. invoke (new Action () => {listBox1.Items. add ("Async .. "+ DateTime. now. toString ("mm: ss fff") ;}); a1.Set ();}
This occurs when you click multiple times. When you click the second time, set () of other threads plays a role for the first time. Return directly without blocking. This is because there is no reset (), so it will not be able to block other methods or threads.




Add a reset to check the status.
            a1.Reset();            bool isback = a1.WaitOne(2000, true);

Mark this. If any error occurs, please correct it. Thank you.

 

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.