ManualResetEvent Class
ManualResetEvent Class
A ManualResetEvent object can posses either one of the two states – signaled (true) or non-signaled (false).
Constructor
Takes in a parameter that affirms the initial state of the object (whether it is signaled or non-signaled).
Usage
ManualResetEvent mansig = new ManualResetEvent(false);
This will create a ManualResetEvent object mansig whose initial state is non-signaled.
bool WaitOne(int32 timeOutMiliseconds, bool exitContext)
When a WaitOne() method is called, it will block (wait) until mansig is signaled (true) or a time value expire. It takes two parameters, the first one is the milliseconds to wait before expiring, and the second boolean parameter to indicate whether we are already in a synchronization domain and want to exit the synchronization context (if you want to reacquire the synchronization context, the exitContext parameter should be set to true). Usually, it is set to false. It returns true if the WaitOne() receives a signal and false if it timeouts. Example,
bool b = mansig.WaitOne(1000, false);
If mansig was defined as above, b will have the value false (because the state of ManualResetEvent is still false) and this WaitOne method call will block for 1 second before returning.
If we change the initial state of the ManualResetEvent to signaled, then the thread does not wait at the WaitOne() method even if a timeout value is specified.
public bool Reset();
To change the state of the ManualResetEvent to non-signaled (false), we call the Reset() method.
public bool Set();
To change the state to signaled, we call the Set() method.
A ManualResetEvent object can posses either one of the two states – signaled (true) or non-signaled (false).
Constructor
Takes in a parameter that affirms the initial state of the object (whether it is signaled or non-signaled).
Usage
ManualResetEvent mansig = new ManualResetEvent(false);
This will create a ManualResetEvent object mansig whose initial state is non-signaled.
bool WaitOne(int32 timeOutMiliseconds, bool exitContext)
When a WaitOne() method is called, it will block (wait) until mansig is signaled (true) or a time value expire. It takes two parameters, the first one is the milliseconds to wait before expiring, and the second boolean parameter to indicate whether we are already in a synchronization domain and want to exit the synchronization context (if you want to reacquire the synchronization context, the exitContext parameter should be set to true). Usually, it is set to false. It returns true if the WaitOne() receives a signal and false if it timeouts. Example,
bool b = mansig.WaitOne(1000, false);
If mansig was defined as above, b will have the value false (because the state of ManualResetEvent is still false) and this WaitOne method call will block for 1 second before returning.
If we change the initial state of the ManualResetEvent to signaled, then the thread does not wait at the WaitOne() method even if a timeout value is specified.
public bool Reset();
To change the state of the ManualResetEvent to non-signaled (false), we call the Reset() method.
public bool Set();
To change the state to signaled, we call the Set() method.

0 Comments:
Post a Comment
<< Home