---
title: "ManualResetEvent"
author: "justdoithz"
date: "2009-01-07T16:51:00Z"
tags: []
slug: "manualresetevent"
draft: false
lastmod: "2009-01-07T16:51:00Z"
id: 898013
url: "https://justdoithz.iteye.com/blog/898013"
---

ManualResetEvent 类

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

**命名空间：** [System.Threading](http://msdn.microsoft.com/zh-cn/library/system.threading.aspx)  
**程序集：** mscorlib（在 mscorlib.dll 中）

![](http://i.msdn.microsoft.com/2ssskfws.alert_note%28zh-cn,VS.90%29.gif "说明")**说明：**

应用到此类型或成员的 [HostProtectionAttribute](http://msdn.microsoft.com/zh-cn/library/system.security.permissions.hostprotectionattribute.aspx) 属性 (Attribute) 具有以下 [Resources](http://msdn.microsoft.com/zh-cn/library/system.security.permissions.hostprotectionattribute.resources.aspx) 属性 (Property) 值：**Synchronization | ExternalThreading**。[HostProtectionAttribute](http://msdn.microsoft.com/zh-cn/library/system.security.permissions.hostprotectionattribute.aspx) 不影响桌面应用程序（这些应用程序通常通过双击图标、键入命令或在浏览器中输入 URL 来启动）。有关更多信息，请参见 [HostProtectionAttribute](http://msdn.microsoft.com/zh-cn/library/system.security.permissions.hostprotectionattribute.aspx) 类或 [SQL Server 编程和宿主保护属性](http://msdn.microsoft.com/zh-cn/library/ms172338.aspx)。

在 .NET Framework 2.0 版中，**ManualResetEvent** 从新的 [EventWaitHandle](http://msdn.microsoft.com/zh-cn/library/system.threading.eventwaithandle.aspx) 类派生。**ManualResetEvent** 在功能上等效于用 [EventResetMode..::.ManualReset](http://msdn.microsoft.com/zh-cn/library/system.threading.eventresetmode.manualreset.aspx) 创建的 [EventWaitHandle](http://msdn.microsoft.com/zh-cn/library/system.threading.eventwaithandle.aspx)。

![](http://i.msdn.microsoft.com/2ssskfws.alert_note%28zh-cn,VS.90%29.gif "说明")**说明：**

与 **ManualResetEvent** 类不同，[EventWaitHandle](http://msdn.microsoft.com/zh-cn/library/system.threading.eventwaithandle.aspx) 类提供对已命名的系统同步事件的访问。

**ManualResetEvent** 允许线程通过发信号互相通信。通常，此通信涉及一个线程在其他线程进行之前必须完成的任务。

当一个线程开始一个活动（此活动必须完成后，其他线程才能开始）时，它调用 [Reset](http://msdn.microsoft.com/zh-cn/library/system.threading.eventwaithandle.reset.aspx) 以将 ManualResetEvent 置于非终止状态。此线程可被视为控制 ManualResetEvent。调用 ManualResetEvent 上的 [WaitOne](http://msdn.microsoft.com/zh-cn/library/system.threading.waithandle.waitone.aspx) 的线程将阻止，并等待信号。当控制线程完成活动时，它调用 [Set](http://msdn.microsoft.com/zh-cn/library/system.threading.eventwaithandle.set.aspx) 以发出等待线程可以继续进行的信号。并释放所有等待线程。

一旦它被终止，ManualResetEvent 将保持终止状态，直到它被手动重置。即对 WaitOne 的调用将立即返回。

可以通过将布尔值传递给构造函数来控制 ManualResetEvent 的初始状态，如果初始状态处于终止状态，为 true；否则为 false。

ManualResetEvent 也可以同 static[WaitAll](http://msdn.microsoft.com/zh-cn/library/system.threading.waithandle.waitall.aspx) 和 [WaitAny](http://msdn.microsoft.com/zh-cn/library/system.threading.waithandle.waitany.aspx) 方法一起使用。

有关线程同步机制的更多信息，请参见概念文档中的 [ManualResetEvent](http://msdn.microsoft.com/zh-cn/library/5hbefs30.aspx)。

![](http://i.msdn.microsoft.com/Global/Images/clear.gif)示例

[&lt;!----&gt;]()

下面的代码示例阐释了如何使用等待句柄来发送复杂数字计算的不同阶段的完成信号。计算采用的格式为：结果 = 第一项 + 第二项 + 第三项，其中每一项都要求使用计算出的基数进行预计算和最终计算。

using System;  
using System.Threading;

class CalculateTest { static void Main()  
{  
Calculate calc = new Calculate(); Console.WriteLine("Result = {0}.",  
calc.Result(234).ToString());  
Console.WriteLine("Result = {0}.",  
calc.Result(55).ToString());  
}  
}

class Calculate  
{  
double baseNumber, firstTerm, secondTerm, thirdTerm;  
AutoResetEvent\[] autoEvents;  
ManualResetEvent manualEvent;

// Generate random numbers to simulate the actual calculations.  
Random randomGenerator;

public Calculate()  
{  
autoEvents = new AutoResetEvent\[]  
{  
new AutoResetEvent(false),  
new AutoResetEvent(false),  
new AutoResetEvent(false)  
};

manualEvent = new ManualResetEvent(false);  
}

void CalculateBase(object stateInfo)  
{  
baseNumber = randomGenerator.NextDouble();

// Signal that baseNumber is ready.  
manualEvent.Set();  
}

// The following CalculateX methods all perform the same  
// series of steps as commented in CalculateFirstTerm.

void CalculateFirstTerm(object stateInfo)  
{  
// Perform a precalculation.  
double preCalc = randomGenerator.NextDouble();

// Wait for baseNumber to be calculated.  
manualEvent.WaitOne();

// Calculate the first term from preCalc and baseNumber.  
firstTerm = preCalc * baseNumber *  
randomGenerator.NextDouble();

// Signal that the calculation is finished.  
autoEvents\[0].Set();  
}

void CalculateSecondTerm(object stateInfo)  
{  
double preCalc = randomGenerator.NextDouble();  
manualEvent.WaitOne();  
secondTerm = preCalc * baseNumber *  
randomGenerator.NextDouble();  
autoEvents\[1].Set();  
}

void CalculateThirdTerm(object stateInfo)  
{  
double preCalc = randomGenerator.NextDouble();  
manualEvent.WaitOne();  
thirdTerm = preCalc * baseNumber *  
randomGenerator.NextDouble();  
autoEvents\[2].Set();  
}

public double Result(int seed)  
{  
randomGenerator = new Random(seed);

// Simultaneously calculate the terms.  
ThreadPool.QueueUserWorkItem(  
new WaitCallback(CalculateBase));  
ThreadPool.QueueUserWorkItem(  
new WaitCallback(CalculateFirstTerm));  
ThreadPool.QueueUserWorkItem(  
new WaitCallback(CalculateSecondTerm));  
ThreadPool.QueueUserWorkItem(  
new WaitCallback(CalculateThirdTerm));

// Wait for all of the terms to be calculated.  
WaitHandle.WaitAll(autoEvents);

// Reset the wait handle for the next calculation.  
manualEvent.Reset();

return firstTerm + secondTerm + thirdTerm;  
}  
}