The difference between c#autoresetevent and ManualResetEvent

Source: Internet
Author: User

One: terminating state and non-terminating state    first of all, the terminating state and the non-terminating state of the thread. In the constructors of AutoResetEvent and ManualResetEvent, there are bool variables to indicate the terminating state and non-terminating state of the thread. True indicates the terminating state (personal understanding is the operational state, which is understood to be the blocking termination of the thread), and False indicates a non-terminating state.
 AutoResetEvent _        AutoResetEvent = new AutoResetEvent (false); private void Bt_temp_click (object sender, RoutedEventArgs e) {thread T1 = new Thread (this.             Thread1foo); T1.             Start (); Thread.Sleep (3000);            Thread.Sleep (Int32) is a batch of current processes suspended for 3000 milliseconds, and is not related to thread T1.         _autoresetevent.set ();             } void Thread1foo () {_autoresetevent.waitone ();         MessageBox.Show ("T1 End");     The result of the execution of this code is that the "T1 end" pops up after 3 seconds.     And if put: autoresetevent _autoresetevent = new AutoResetEvent (false);     instead: AutoResetEvent _autoresetevent = new AutoResetEvent (true);     The "T1 end" will pop up immediately. That is, in the terminating state, _autoresetevent.waitone () does not act as a block to the worker thread. (Ps:manualresetevent also) two: the difference between AutoResetEvent and ManualResetEvent 

autoresetevent Allows threads to communicate with all other by signaling. " >       AutoResetEvent  allow threads to communicate with each other by signaling.   Typically, this class is used when the thread needs exclusive access to the resource.

WaitOne on the AutoResetEvent . " >        threads wait for a signal by calling  WaitOne  on  autoresetevent .  autoresetevent is in the non-signaled state, the thread blocks, waiting for the thre Ad that currently controls the resource to signal, the resource is available by calling Set ." > if  autoresetevent  is a non-terminating state , the thread is blocked and waits for the thread of the current control resource to call   Set to notify the resource to be available.

CallSet toThe AutoResetEvent sends a signal to release the waiting thread.AutoResetEvent will remainTermination StatusUntil a waiting thread is freed and then automatically returnednon-terminating status。If no thread is waiting, the state will remain indefinitely toTermination Status。 WaitOne while the autoresetevent is in the signaled state, and the thread does not block." > If the thread calls  waitone when  autoresetevent  is terminating state , the thread is not blocked.  autoresetevent releases the thread immediately and returns to the non-signaled State. " >autoresetevent  will immediately release the thread and return to the non-terminating state .

WaitOne While the autoresetevent are in the signaled state, the thread does n OT block. " >autoresetevent releases the thread immediately and returns to the non-signaled state. " > 

     AutoResetEvent _autoresetevent = new AutoResetEvent (false); private void Bt_temp_click (object sender, RoutedEventArgs e) {thread T1 = new Thread (this.             Thread1foo); T1.             Start (); Thread t2 = new Thread (this.             Thread2foo); T2.             Start ();             Thread.Sleep (3000);         _autoresetevent.set ();             } void Thread1foo () {_autoresetevent.waitone ();         MessageBox.Show ("T1 End");             } void Thread2foo () {_autoresetevent.waitone ();         MessageBox.Show ("T2 End"); The effect of this code operation is that after 3 seconds, either pops up "T1 End" or pops up "T2 end", and no two pops up. In other words, one of them will end, and the other process never ends.        Code snippet 3:manualresetevent _menurestevent = new ManualResetEvent (false); private void Bt_temp_click (object sender, RoutedEventArgs e) {thread T1 = new Thread (this.             Thread1foo); T1.             Start (); Thread t2 = new Thread (this.            Thread2foo); T2.             Start ();             Thread.Sleep (3000);         _menurestevent.set ();             } void Thread1foo () {_menurestevent.waitone ();         MessageBox.Show ("T1 End");             } void Thread2foo () {_menurestevent.waitone ();         MessageBox.Show ("T2 End"); The effect of this code operation is that after 3 seconds, "T1 end" and "T2 End", two are ejected.     In other words, two processes are over. This feature means that AutoResetEvent only sends a signal to one thread, not to multiple threads. When we need to synchronize multiple threads, we can only use ManualResetEvent. The deep-seated reason is that after set (), the AutoResetEvent automatically sets the thread state to false, and after the ManualResetEvent is set (), the state of the thread becomes true and after reset () must be manually The thread will be reset to false again. That's why their names are auto, one for manual.        To more fully verify this feature of Manua lresetevent, let's look at the code Snippet 4 code snippet 4:manualresetevent _menurestevent = new ManualResetEvent (false); private void Bt_temp_click (object sender, RoutedEventArgs e) {thread T1 = new Thread (this.             Thread1foo); T1.             Start (); Thread t2 = new Thread (this.             Thread2foo); T2.             Start (); ThRead.             Sleep (3000);             _menurestevent.set ();                    _menurestevent.reset ();             } void Thread1foo () {_menurestevent.waitone ();             MessageBox.Show ("T1 Step1 end");             Sleep 1S, used to wait for the main thread _menurestevent.reset ();             Thread.Sleep (1000);             _menurestevent.waitone ();         MessageBox.Show ("T1 Step2 end");             } void Thread2foo () {_menurestevent.waitone ();             MessageBox.Show ("T2 Step1 End");             Sleep 1S, used to wait for the main thread _menurestevent.reset ();             Thread.Sleep (1000);             _menurestevent.waitone ();         MessageBox.Show ("T2 Step2 End"); In code Snippet 4, we commented on//_menurestevent.reset (), that is, after _menurestevent.set (), the state of the thread is true, and the program runs with the result "T1 Step1 end", "T1    Step2 end "," T1 Step2 end "," T2 Step2 End "All pop up after 3 seconds. And if we remove the comments from//_menurestevent.reset (), we'll see that "T1 Step2 end" and "T2 Step2 end" never pop up. Unless we set () the _menurestevent again in the main thread.

  

The difference between c#autoresetevent and ManualResetEvent

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.