C#中ManualResetEvent用法总结

发布时间:2026-09-28 18:03:35 浏览次数:0

下面将为您详细讲解C#中ManualResetEvent用法总结的完整攻略。

什么是ManualResetEvent?

ManualResetEvent是System.Threading命名空间中的一个类,是用于线程同步的一种工具。它可以让线程之间进行信号通讯,实现线程之间的同步和互斥。通常情况下,ManualResetEvent作为线程协调的一种机制,可以帮助我们实现多线程之间的通信和协调。

ManualResetEvent的用法总结

在这里,我们将总结ManualResetEvent的基本用法,主要包括ManualResetEvent的创建、设置以及维护等方面的内容。

ManualResetEvent的创建

创建ManualResetEvent比较简单,在创建ManualResetEvent之前,需要引入System.Threading命名空间。创建ManualResetEvent的代码示例如下所示:

using System.Threading;...ManualResetEvent manualResetEvent = new ManualResetEvent(false); 

ManualResetEvent的设置

设置ManualResetEvent通常是通过Set()方法完成,其方法原型如下:

public void Set();

Set()方法是一个实例方法,调用它后,ManualResetEvent的状态将变为“终止状态”,在“终止状态”下,ManualResetEvent将不再阻塞任何一个等待线程,所有等待线程将得到无条件的通知,继续执行下去。

using System.Threading;using System.Threading.Tasks;using System.Diagnostics;...ManualResetEvent manualResetEvent = new ManualResetEvent(false); //处于未终止状态Task.Factory.StartNew(() =>{    Console.WriteLine("Start");    manualResetEvent.WaitOne(); //等待ManualResetEvent的信号    Console.WriteLine("End");});Thread.Sleep(1000);manualResetEvent.Set(); //置ManualResetEvent状态为终止状态

ManualResetEvent的重置

重置ManualResetEvent是通过Reset()方法完成,其方法原型如下:

public void Reset();

和Set()方法类似,Reset()方法也是一个实例方法,在调用它之后,ManualResetEvent的状态将被重置为“未终止状态”,也就是说,ManualResetEvent又可用于阻塞线程。

using System.Threading;using System.Threading.Tasks;using System.Diagnostics;...ManualResetEvent manualResetEvent = new ManualResetEvent(false); //处于未终止状态Task.Factory.StartNew(() =>{    Console.WriteLine("Start");    manualResetEvent.WaitOne(); //等待ManualResetEvent的信号    Console.WriteLine("End");});Thread.Sleep(1000);manualResetEvent.Set(); //置ManualResetEvent状态为终止状态manualResetEvent.Reset(); //置ManualResetEvent状态为非终止状态

ManualResetEvent的状态查询

ManualResetEvent的状态查询是通过WaitOne()方法完成,其方法原型如下:

public bool WaitOne(int millisecondsTimeout);

WaitOne()方法是一个实例方法,它可以阻塞等待等待ManualResetEvent的信号,如果在等待过程中ManualResetEvent的状态变为了“终止状态”,WaitOne()方法会立即返回true;如果在等待过程中超时或收到中断信号,则WaitOne()方法返回false。

using System.Threading;using System.Diagnostics;...ManualResetEvent manualResetEvent = new ManualResetEvent(false); //处于未终止状态Stopwatch stopwatch = new Stopwatch();stopwatch.Start();bool result = manualResetEvent.WaitOne(1000);stopwatch.Stop();Console.WriteLine(result);Console.WriteLine(stopwatch.ElapsedMilliseconds);

示例说明

下面我们通过两个示例说明ManualResetEvent的用法。

示例1:ManualResetEvent的基本用法

在这个示例中,我们将使用ManualResetEvent来控制一个任务的执行,当ManualResetEvent接收到信号时,该任务会开始执行。这个示例中,我们创建了一个ManualResetEvent对象,并将其初始状态置为“未终止状态”。然后通过一个任务,在一秒后打印“End”的信息。此时ManualResetEvent的状态还是“未终止状态”,任务会被阻塞住。接着我们更新ManualResetEvent的状态为“终止状态”,任务便可以开始执行了。

using System.Threading;using System.Threading.Tasks;using System.Diagnostics;...ManualResetEvent manualResetEvent = new ManualResetEvent(false); //处于未终止状态Task.Factory.StartNew(() =>{    Console.WriteLine("Start");    manualResetEvent.WaitOne(); //等待ManualResetEvent的信号    Console.WriteLine("End");});Thread.Sleep(1000);manualResetEvent.Set(); //置ManualResetEvent状态为终止状态

示例2:ManualResetEvent的超时控制

在这个示例中,我们使用ManualResetEvent对象来进行超时控制。首先我们执行ManualResetEvent的等待方法,但是等待的时间设置为了1000ms,也就是1秒,如果在1秒内ManualResetEvent的状态仍旧没有变为“终止状态”,那么WaitOne()方法将返回false。在这个示例中,我们使用了Stopwatch来计算等待的时间,最后输出等待的结果和用时。

using System.Threading;using System.Diagnostics;...ManualResetEvent manualResetEvent = new ManualResetEvent(false); //处于未终止状态Stopwatch stopwatch = new Stopwatch();stopwatch.Start();bool result = manualResetEvent.WaitOne(1000);stopwatch.Stop();Console.WriteLine(result);Console.WriteLine(stopwatch.ElapsedMilliseconds);

总结

需要做网站?需要网络推广?欢迎咨询客户经理 13272073477