[c#基础]AutoResetEvent

时间:2023-12-13 09:54:02

摘要

AutoResetEvent:msdn的描述是通知正在等待的线程已发生事件。此类不能被继承。也就是说它有那么一个时间点,会通知正在等待的线程可以做其它的事情了。

AutoResetEvent

该类有一个带bool类型参数的构造函数

#region Assembly mscorlib.dll, v4.0.0.0
// C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\mscorlib.dll
#endregion using System;
using System.Runtime.InteropServices; namespace System.Threading
{
// Summary:
// Notifies a waiting thread that an event has occurred. This class cannot be
// inherited.
[ComVisible(true)]
public sealed class AutoResetEvent : EventWaitHandle
{
// Summary:
// Initializes a new instance of the System.Threading.AutoResetEvent class with
// a Boolean value indicating whether to set the initial state to signaled.
//
// Parameters:
// initialState:
// true to set the initial state to signaled; false to set the initial state
// to non-signaled.
public AutoResetEvent(bool initialState);
}
}

该bool值指示初始化的时候是否设置为终止状态。

AutoResetEvent allows threads to communicate with each other by signaling.Typically, you use this class when threads need exclusive access to a resource.

AutoResetEvent允许线程之间通过信号进行通信。通常,当线程独占访问资源的时候你可以使用该类。

注意

This type implements the IDisposable interface.When you have finished using the type, you should dispose of it either directly or indirectly.To dispose of the type directly, call its Dispose method in a try/catch block.

该类实现了IDisposable接口。当你使用结束的时候,你需要直接或者间接的释放它。直接释放可以通过在try/catch块中调用它的Dispose方法。

一个线程等待在AutoResetEvent上调用WaitOne信号。如果AutoResetEvent为未触发状态,则线程会被阻止,并等待当前控制资源的线程通过调用Set来通知资源可用。

调用Set信号,AutoResetEvent将释放一个等待中的线程。当AutoResetEvent被设置为已触发状态时,它将一直保持已触发状态直到一个等待的线程被激活,然后它将自动编程未触发状态。如果没有任何线程在等待,则状态将无限期地保持为已触发状态。

如果当AutoResetEvent为已触发状态时调用WaitOne,则线程不会被阻止。AutoResetEvent将立即释放线程并返回到未触发状态。

There is no guarantee that every call to the Set method will release a thread.If two calls are too close together, so that the second call occurs before a thread has been released, only one thread is released.It is as if the second call did not happen.Also, if Set is called when there are no threads waiting and the AutoResetEvent is already signaled, the call has no effect.

也不能保证每次调用set方法就释放一个线程。如果两次调用太近,以至于第二次调用在释放线程时,只有一个线程被释放。就好像第二次调用没发生一样。另外,如果设置为当没有线程等待和AutoResetEvent已经发出信号,则调用没有影响。

你可以通过构造函数的bool值参数控制初始化时AutoRestEvent的状态。若要将初始状态设置为终止,则为 true;若要将初始状态设置为非终止,则为 false。

示例

下面的示例将演示如何使用AutoResetEvent通过Set方法在用户按下回车键的时候释放一个线程。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks; namespace Wolfy.AutoResetEventDemo
{ class Program
{
const int numIterations = ;
//初始化为非终止状态。
static AutoResetEvent myResetEvent = new AutoResetEvent(false);
static int number; static void Main(string[] args)
{
//创建并启动读线程
Thread myReaderThread = new Thread(new ThreadStart(MyReaderThreadProc));
myReaderThread.Name = "ReaderThread";
myReaderThread.Start();
for (int i = ; i < numIterations; i++)
{
Console.WriteLine("Writer thread writing value:{0}", i);
number = i;
//给读线程信号
myResetEvent.Set();
Thread.Sleep();
}
myReaderThread.Abort();
} private static void MyReaderThreadProc()
{
while (true)
{
myResetEvent.WaitOne();
Console.WriteLine("{0} reading value:{1}", Thread.CurrentThread.Name, number);
}
}
}
}

执行顺序

[c#基础]AutoResetEvent

运行结果

[c#基础]AutoResetEvent