ManualResetEvent 类

通知一个或多个正在等待的线程已发生事件。 此类不能被继承。

命名空间:                   System.Threading
程序集:         mscorlib(位于 mscorlib.dll)

继承层次结构

System.Object
System.MarshalByRefObject
System.Threading.WaitHandle
System.Threading.EventWaitHandle
System.Threading.ManualResetEvent

语法

[ComVisibleAttribute(true)]
[HostProtectionAttribute(SecurityAction.LinkDemand, Synchronization = true, 
ExternalThreading = true)]
public sealed class ManualResetEvent : EventWaitHandle

构造函数


名称说明
System_CAPS_pubmethodManualResetEvent(Boolean)

新实例初始化 ManualResetEvent 使用 Boolean 值,该值指示是否将初始状态设置为终止状态的类。

属性


名称说明
System_CAPS_pubpropertyHandle

已过时。            获取或设置本机操作系统句柄。(继承自 WaitHandle。)

System_CAPS_pubpropertySafeWaitHandle

获取或设置本机操作系统句柄。(继承自 WaitHandle。)

方法


名称说明
System_CAPS_pubmethodClose()

释放由当前 WaitHandle 占用的所有资源。(继承自 WaitHandle。)

System_CAPS_pubmethodCreateObjRef(Type)

创建包含生成用来与远程对象通信的代理所需的所有相关信息的对象。(继承自 MarshalByRefObject。)

System_CAPS_pubmethodDispose()

释放 WaitHandle 类的当前实例所使用的所有资源。(继承自 WaitHandle。)

System_CAPS_pubmethodEquals(Object)

确定指定的对象是否等于当前对象。(继承自 Object。)

System_CAPS_pubmethodGetAccessControl()

获取一个 EventWaitHandleSecurity 对象,它表示当前 EventWaitHandle 对象所表示的已命名系统事件的访问控件安全性。(继承自 EventWaitHandle。)

System_CAPS_pubmethodGetHashCode()

作为默认哈希函数。(继承自 Object。)

System_CAPS_pubmethodGetLifetimeService()

检索当前生存期的服务对象,用于控制此实例的生存期策略。(继承自 MarshalByRefObject。)

System_CAPS_pubmethodGetType()

获取当前实例的 Type(继承自 Object。)

System_CAPS_pubmethodInitializeLifetimeService()

获取使用生存期服务对象来控制此实例的生存期策略。(继承自 MarshalByRefObject。)

System_CAPS_pubmethodReset()

将事件状态设置为非终止,从而导致线程受阻。(继承自 EventWaitHandle。)

System_CAPS_pubmethodSet()

将事件状态设置为有信号,从而允许一个或多个等待线程继续执行。(继承自 EventWaitHandle。)

System_CAPS_pubmethodSetAccessControl(EventWaitHandleSecurity)

设置已命名的系统事件的访问控制安全性。(继承自 EventWaitHandle。)

System_CAPS_pubmethodToString()

返回表示当前对象的字符串。(继承自 Object。)

System_CAPS_pubmethodWaitOne()

阻止当前线程,直到当前 WaitHandle 收到信号。(继承自 WaitHandle。)

System_CAPS_pubmethodWaitOne(Int32)

阻止当前线程,直到当前 WaitHandle 收到信号,同时使用 32 位带符号整数指定时间间隔(以毫秒为单位)。(继承自 WaitHandle。)

System_CAPS_pubmethodWaitOne(Int32,Boolean)

阻止当前线程,直到当前的 WaitHandle 收到信号为止,同时使用 32 位带符号整数指定时间间隔,并指定是否在等待之前退出同步域。(继承自 WaitHandle。)

System_CAPS_pubmethodWaitOne(TimeSpan)

阻止当前线程,直到当前实例收到信号,同时使用 TimeSpan 指定时间间隔。(继承自 WaitHandle。)

System_CAPS_pubmethodWaitOne(TimeSpan,Boolean)

阻止当前线程,直到当前实例收到信号为止,同时使用 TimeSpan 指定时间间隔,并指定是否在等待之前退出同步域。(继承自 WaitHandle。)


扩展方法


名称说明
System_CAPS_pubmethodGetSafeWaitHandle()

获取本机操作系统等待句柄的安全句柄。(由 WaitHandleExtensions 定义。)

System_CAPS_pubmethodSetSafeWaitHandle(SafeWaitHandle)

设置本机操作系统等待句柄的安全句柄。(由 WaitHandleExtensions 定义。)

备注

在.NET Framework 2.0 版中, ManualResetEvent 派生自新 EventWaitHandle 类。 一个 ManualResetEvent 功能上等效于 EventWaitHandle 使用创建 EventResetMode.ManualReset

System_CAPS_note说明

与不同 ManualResetEvent 类, EventWaitHandle 类提供了对访问已命名的系统同步事件。

 ManualResetEvent 允许通过发送信号相互进行通信的线程。 通常情况下,此通信涉及其他线程可以继续操作之前,必须完成的一个线程的任务。

当线程开始其他之前,必须完成的活动线程继续时,它调用 Reset 放置 ManualResetEvent 处于非终止状态。 此线程可以被视为控制 ManualResetEvent 线程调用 WaitOneManualResetEvent 将阻止,请等待信号。 当控制线程完成活动时,它将调用 Set 发出信号等待线程可以继续执行。 将释放所有等待的线程。

一旦它被终止, ManualResetEvent 手动重置之前一直保持终止状态。 也就是说,调用 WaitOne 立即返回。

您可以控制的初始状态 ManualResetEvent 通过将一个布尔值传递给构造函数中, true 如果初始状态终止状态和 false 否则为。

 ManualResetEvent 也可以用于 staticWaitAllWaitAny 方法。

有关线程同步机制的详细信息,请参阅 ManualResetEvent and ManualResetEventSlim 的概念文档中。

示例

下面的示例演示如何 ManualResetEvent 工作原理。 此示例将启动与 ManualResetEvent 中未终止的状态 (即, false 传递给构造函数)。 该示例创建了三个线程时,每个哪些块上 ManualResetEvent 通过调用其 WaitOne 方法。 当用户按 Enter 键,该示例通过调用 Set 方法,释放所有三个线程。 与此相反的行为 AutoResetEvent 类,该类在每次发布后自动重置一次释放一个线程。

Enter 密钥再次表明 ManualResetEvent 处于终止状态,直到其 Reset 调用方法︰ 此示例将启动两个线程。 拨打电话时不会阻止这些线程 WaitOne 方法,而是会运行直至完成。

Enter 密钥再次使该示例调用 Reset 方法,并启动一个更多的线程,它将调用操作被阻塞 WaitOneEnter 密钥之一最后一次调用 Set 来释放最后一个线程和该程序结束。

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.");
    }
}

/* This example produces output similar to the following:
Start 3 named threads that block on a ManualResetEvent:
Thread_0 starts and calls mre.WaitOne()
Thread_1 starts and calls mre.WaitOne()
Thread_2 starts and calls mre.WaitOne()
When all three threads have started, press Enter to call Set()
to release all the threads.
Thread_2 ends.
Thread_0 ends.
Thread_1 ends.
When a ManualResetEvent is signaled, threads that call WaitOne()
do not block. Press Enter to show this.
Thread_3 starts and calls mre.WaitOne()
Thread_3 ends.
Thread_4 starts and calls mre.WaitOne()
Thread_4 ends.
Press Enter to call Reset(), so that threads once again block
when they call WaitOne().

Thread_5 starts and calls mre.WaitOne()

Press Enter to call Set() and conclude the demo.

Thread_5 ends.
 */

备注:摘自https://msdn.microsoft.com/zh-cn/library/system.threading.manualresetevent.aspx