C # ManualResetEvent

Source: Internet
Author: User

Original link Http://dotnetpattern.com/threading-manualresetevent

ManualResetEvent, like AutoResetEvent, is another. NET thread synchronization technology.


The ManualResetEvent is used to send thread signals between two or more threads.


Multiple threads can enter a wait or block state by calling the WaitOne method of the ManualResetEvent object. When the control thread calls the set () method, all the waiting threads resume and continue execution.

How the ManualResetEvent works

A bool value is maintained in memory, and if the bool value is false, all threads are blocked, and conversely, if the bool value is true, all the threads exit blocking. When we create an instance of a ManualResetEvent object, we pass the default bool value in the function construct, and the following is an example of instantiating ManualResetEvent.

New ManualResetEvent (false);

In the above code, we initialized a ManualResetEvent object with a value of false, which means that all threads that call WaitOne will be blocked until the Set () method is called by the thread thread. And if we initialize the ManualResetEvent object with a value of true, all threads that call the WaitOne method are not blocked and can be executed later.

WaitOne method

This method blocks the current thread and waits for other threads to send a signal. If a signal is received, it returns true, whereas the reverse returns false. The following shows how to call the method.

Manualresetevent.waitone ();

In the second overloaded version of the WaitOne method, we can specify the time interval at which the current thread waits for a signal. If no signal is received within the time interval, the method returns false and continues execution. The following code shows a WaitOne call with a time interval parameter.

bool issignalled = Manualresetevent.waitone (Timespan.fromseconds (5));

We specify 5 seconds as a parameter to the WaitOne method, and if the ManualResetEvent object receives a signal within 5 seconds, it assigns the issignalled value to False.

Set method

This method is used to send signals to all waiting threads. The call to the Set () method causes the bool variable value of the ManualResetEvent object to be true, and all threads are freed and continue to execute. The following is an example of a call:

Manualresetevent.set ();

Reset Method
Once we call the set () method of the ManualResetEvent object, its bool value becomes true, we can call the Reset () method to reset the value, and the Reset () method resets the value to False. The following is an example of calling the Reset method:
Manualresetevent.reset ();

If we want to send a signal multiple times, we must call the Reset () method immediately after calling the set () method.

ManualResetEvent Example
The following example shows how to use ManualResetEvent to release multiple threads. We instantiate the ManualResetEvent object with a value of false, which blocks all threads that call the WaitOne method. We created two threads that call the method Getdatafromserver and take the number of servers as parameters.
After calling the WaitOne method to get the first batch number, two threads are waiting for a signal from the calling WaitOne thread. When the control thread invokes the set method of the Manualrestevent object, two threads are freed and continue to run. After calling the set method, we immediately call the Reset method, which changes the bool value of the Manualrestevent object to False. So, if the thread calls the WaitOne method again, they will still be blocked.
After obtaining the second batch of data from the server, two threads call the WaitOne method. After 2 seconds, the control thread calls the set method again to release two threads.

classprogram{StaticManualResetEvent ManualResetEvent =NewManualResetEvent (false); Static voidMain (string[] args) {Task Task= Task.Factory.StartNew (() ={getdatafromserver (1);         }); Task.Factory.StartNew (()={getdatafromserver (2);          }); //Send first signal to get first set of data from server 1 and server 2Manualresetevent.set ();         Manualresetevent.reset (); Thread.Sleep (Timespan.fromseconds (2)); //Send second signal to get second set of data from server 1 and server 2Manualresetevent.set ();         Console.ReadLine (); /*Result * I get First data from Server1 * I get First data from Server2 * I get Secon            D Data from Server1 * I get second data from Server2 * All the data collected from Server2 * All the data collected from Server1*/    }     Static voidGetdatafromserver (intServernumber) {        //calling any webservice to get dataConsole.WriteLine ("I get First data from server"+servernumber);         Manualresetevent.waitone (); Thread.Sleep (Timespan.fromseconds (2)); Console.WriteLine ("I get second data from server"+servernumber);        Manualresetevent.waitone (); Console.WriteLine ("All the data collected from server"+servernumber); }}

C # ManualResetEvent

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.