ManualResetEvent Class

 Come form MSDN

 

ManualResetEvent allows threads to communicate with each other by signaling. Typically, this communication concerns a task which one thread must complete before other threads can proceed.

When a thread begins an activity that must complete before other threads proceed, it calls Reset to put ManualResetEvent in the non-signaled state. This thread can be thought of as controlling the ManualResetEvent . Threads that call WaitOne on the ManualResetEvent will block, awaiting the signal. When the controlling thread completes the activity, it calls Set to signal that the waiting threads can proceed. All waiting threads are released.

Once it has been signaled, ManualResetEvent remains signaled until it is manually reset. That is, calls to WaitOne return immediately.

You can control the initial state of a ManualResetEvent by passing a Boolean value to the constructor, true if the initial state is signaled and false otherwise.

ManualResetEvent can also be used with the static WaitAll and WaitAny methods.

 
Examples

The following example demonstrates how ManualResetEvent works. The example starts with a ManualResetEvent in the unsignaled state (that is, false is passed to the constructor). The example creates three threads, each of which blocks on the ManualResetEvent by calling its WaitOne method. When you press the Enter key, the example calls the Set method, which releases all three threads. Contrast this with the behavior of the AutoResetEvent class, which releases threads one at a time, resetting automatically after each release.

Pressing the Enter key again demonstrates that the ManualResetEvent remains in the signaled state until its Reset method is called: The example starts two more threads. These threads do not block when they call the WaitOne method, but instead run to completion.

Pressing the Enter key again causes the example to call the Reset() method and to start one more thread, which blocks when it calls WaitOne . Pressing the Enter key one final time calls Set to release the last thread, and the program ends.

 

  using System;
using System.Threading;

public class Example
{
// mre is used to block and release threads manually. It is
// created in the unsignaled state.
private static ManualResetEvent mre = new ManualResetEvent(false);

static void Main()
{
Console.WriteLine("/nStart 3 named threads that block on a ManualResetEvent:/n");

for(int i = 0; i <= 2; i++)
{
Thread t = new Thread(ThreadProc);
t.Name = "Thread_" + i;
t.Start();
}

Thread.Sleep(500);
Console.WriteLine("/nWhen all three threads have started, press Enter to call Set()" +
"/nto release all the threads./n");
Console.ReadLine();

mre.Set();

Thread.Sleep(500);
Console.WriteLine("/nWhen a ManualResetEvent is signaled, threads that call WaitOne()" +
"/ndo not block. Press Enter to show this./n");
Console.ReadLine();

for(int i = 3; i <= 4; i++)
{
Thread t = new Thread(ThreadProc);
t.Name = "Thread_" + i;
t.Start();
}

Thread.Sleep(500);
Console.WriteLine("/nPress Enter to call Reset(), so that threads once again block" +
"/nwhen they call WaitOne()./n");
Console.ReadLine();

mre.Reset();

// Start a thread that waits on the ManualResetEvent.
Thread t5 = new Thread(ThreadProc);
t5.Name = "Thread_5";
t5.Start();

Thread.Sleep(500);
Console.WriteLine("/nPress Enter to call Set() and conclude the demo.");
Console.ReadLine();

mre.Set();

// If you run this example in Visual Studio, uncomment the following line:
//Console.ReadLine();
}


private static void ThreadProc()
{
string name = Thread.CurrentThread.Name;

Console.WriteLine(name + " starts and calls mre.WaitOne()");

mre.WaitOne();

Console.WriteLine(name + " ends.");
}
}


-----------
Conclude By myself

if a instance of
ManualResetEvent class have no signaled ,then if we invoke
WaitOne() method ,that thread will block ,until this instance get a signal .
in compare , if a instance of ManualResetEvent class is signaled state ,this thread will not bolck when we invoke WaitOne()
method .


ManualResetEvent mre = new ManualResetEvent(false); //no signal
ManualResetEvent mre = new ManualResetEvent(false); // have signaled
mre.Set(); // have signa
mre.Reset() // no sign


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值