AutoResetEvent 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示一个线程同步事件,该事件在发出信号时释放一个等待线程,然后自动重置。 此类不能被继承。
public ref class AutoResetEvent sealed : System::Threading::EventWaitHandle
public ref class AutoResetEvent sealed : System::Threading::WaitHandle
public sealed class AutoResetEvent : System.Threading.EventWaitHandle
public sealed class AutoResetEvent : System.Threading.WaitHandle
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class AutoResetEvent : System.Threading.EventWaitHandle
type AutoResetEvent = class
inherit EventWaitHandle
type AutoResetEvent = class
inherit WaitHandle
[<System.Runtime.InteropServices.ComVisible(true)>]
type AutoResetEvent = class
inherit EventWaitHandle
Public NotInheritable Class AutoResetEvent
Inherits EventWaitHandle
Public NotInheritable Class AutoResetEvent
Inherits WaitHandle
- 继承
- 继承
- 继承
- 属性
示例
下面的示例演示如何在AutoResetEventSet每次用户按 Enter 键时调用基类 (方法) 一次释放一个线程。 该示例启动三个 AutoResetEvent 线程,等待在信号状态下创建的 。 第一个线程会立即释放,因为 AutoResetEvent 已处于信号状态。 这会将 重置 AutoResetEvent 为非信号状态,以便后续线程阻塞。 在用户通过按 Enter 键一次释放一个线程之前,不会释放阻塞的线程。
从第一个 AutoResetEvent释放线程后,它们等待另一个 AutoResetEvent 以非信号状态创建的线程。 所有三个线程都阻塞,因此 Set 必须调用方法三次才能释放它们。
using namespace System;
using namespace System::Threading;
ref class Example
{
private:
static AutoResetEvent^ event_1 = gcnew AutoResetEvent(true);
static AutoResetEvent^ event_2 = gcnew AutoResetEvent(false);
static void ThreadProc()
{
String^ name = Thread::CurrentThread->Name;
Console::WriteLine("{0} waits on AutoResetEvent #1.", name);
event_1->WaitOne();
Console::WriteLine("{0} is released from AutoResetEvent #1.", name);
Console::WriteLine("{0} waits on AutoResetEvent #2.", name);
event_2->WaitOne();
Console::WriteLine("{0} is released from AutoResetEvent #2.", name);
Console::WriteLine("{0} ends.", name);
}
public:
static void Demo()
{
Console::WriteLine("Press Enter to create three threads and start them.\r\n" +
"The threads wait on AutoResetEvent #1, which was created\r\n" +
"in the signaled state, so the first thread is released.\r\n" +
"This puts AutoResetEvent #1 into the unsignaled state.");
Console::ReadLine();
for (int i = 1; i < 4; i++)
{
Thread^ t = gcnew Thread(gcnew ThreadStart(&ThreadProc));
t->Name = "Thread_" + i;
t->Start();
}
Thread::Sleep(250);
for (int i = 0; i < 2; i++)
{
Console::WriteLine("Press Enter to release another thread.");
Console::ReadLine();
event_1->Set();
Thread::Sleep(250);
}
Console::WriteLine("\r\nAll threads are now waiting on AutoResetEvent #2.");
for (int i = 0; i < 3; i++)
{
Console::WriteLine("Press Enter to release a thread.");
Console::ReadLine();
event_2->Set();
Thread::Sleep(250);
}
// Visual Studio: Uncomment the following line.
//Console::Readline();
}
};
void main()
{
Example::Demo();
}
/* This example produces output similar to the following:
Press Enter to create three threads and start them.
The threads wait on AutoResetEvent #1, which was created
in the signaled state, so the first thread is released.
This puts AutoResetEvent #1 into the unsignaled state.
Thread_1 waits on AutoResetEvent #1.
Thread_1 is released from AutoResetEvent #1.
Thread_1 waits on AutoResetEvent #2.
Thread_3 waits on AutoResetEvent #1.
Thread_2 waits on AutoResetEvent #1.
Press Enter to release another thread.
Thread_3 is released from AutoResetEvent #1.
Thread_3 waits on AutoResetEvent #2.
Press Enter to release another thread.
Thread_2 is released from AutoResetEvent #1.
Thread_2 waits on AutoResetEvent #2.
All threads are now waiting on AutoResetEvent #2.
Press Enter to release a thread.
Thread_2 is released from AutoResetEvent #2.
Thread_2 ends.
Press Enter to release a thread.
Thread_1 is released from AutoResetEvent #2.
Thread_1 ends.
Press Enter to release a thread.
Thread_3 is released from AutoResetEvent #2.
Thread_3 ends.
*/
using System;
using System.Threading;
// Visual Studio: Replace the default class in a Console project with
// the following class.
class Example
{
private static AutoResetEvent event_1 = new AutoResetEvent(true);
private static AutoResetEvent event_2 = new AutoResetEvent(false);
static void Main()
{
Console.WriteLine("Press Enter to create three threads and start them.\r\n" +
"The threads wait on AutoResetEvent #1, which was created\r\n" +
"in the signaled state, so the first thread is released.\r\n" +
"This puts AutoResetEvent #1 into the unsignaled state.");
Console.ReadLine();
for (int i = 1; i < 4; i++)
{
Thread t = new Thread(ThreadProc);
t.Name = "Thread_" + i;
t.Start();
}
Thread.Sleep(250);
for (int i = 0; i < 2; i++)
{
Console.WriteLine("Press Enter to release another thread.");
Console.ReadLine();
event_1.Set();
Thread.Sleep(250);
}
Console.WriteLine("\r\nAll threads are now waiting on AutoResetEvent #2.");
for (int i = 0; i < 3; i++)
{
Console.WriteLine("Press Enter to release a thread.");
Console.ReadLine();
event_2.Set();
Thread.Sleep(250);
}
// Visual Studio: Uncomment the following line.
//Console.Readline();
}
static void ThreadProc()
{
string name = Thread.CurrentThread.Name;
Console.WriteLine("{0} waits on AutoResetEvent #1.", name);
event_1.WaitOne();
Console.WriteLine("{0} is released from AutoResetEvent #1.", name);
Console.WriteLine("{0} waits on AutoResetEvent #2.", name);
event_2.WaitOne();
Console.WriteLine("{0} is released from AutoResetEvent #2.", name);
Console.WriteLine("{0} ends.", name);
}
}
/* This example produces output similar to the following:
Press Enter to create three threads and start them.
The threads wait on AutoResetEvent #1, which was created
in the signaled state, so the first thread is released.
This puts AutoResetEvent #1 into the unsignaled state.
Thread_1 waits on AutoResetEvent #1.
Thread_1 is released from AutoResetEvent #1.
Thread_1 waits on AutoResetEvent #2.
Thread_3 waits on AutoResetEvent #1.
Thread_2 waits on AutoResetEvent #1.
Press Enter to release another thread.
Thread_3 is released from AutoResetEvent #1.
Thread_3 waits on AutoResetEvent #2.
Press Enter to release another thread.
Thread_2 is released from AutoResetEvent #1.
Thread_2 waits on AutoResetEvent #2.
All threads are now waiting on AutoResetEvent #2.
Press Enter to release a thread.
Thread_2 is released from AutoResetEvent #2.
Thread_2 ends.
Press Enter to release a thread.
Thread_1 is released from AutoResetEvent #2.
Thread_1 ends.
Press Enter to release a thread.
Thread_3 is released from AutoResetEvent #2.
Thread_3 ends.
*/
Imports System.Threading
' Visual Studio: Replace the default class in a Console project with
' the following class.
Class Example
Private Shared event_1 As New AutoResetEvent(True)
Private Shared event_2 As New AutoResetEvent(False)
<MTAThread()> _
Shared Sub Main()
Console.WriteLine("Press Enter to create three threads and start them." & vbCrLf & _
"The threads wait on AutoResetEvent #1, which was created" & vbCrLf & _
"in the signaled state, so the first thread is released." & vbCrLf & _
"This puts AutoResetEvent #1 into the unsignaled state.")
Console.ReadLine()
For i As Integer = 1 To 3
Dim t As New Thread(AddressOf ThreadProc)
t.Name = "Thread_" & i
t.Start()
Next
Thread.Sleep(250)
For i As Integer = 1 To 2
Console.WriteLine("Press Enter to release another thread.")
Console.ReadLine()
event_1.Set()
Thread.Sleep(250)
Next
Console.WriteLine(vbCrLf & "All threads are now waiting on AutoResetEvent #2.")
For i As Integer = 1 To 3
Console.WriteLine("Press Enter to release a thread.")
Console.ReadLine()
event_2.Set()
Thread.Sleep(250)
Next
' Visual Studio: Uncomment the following line.
'Console.Readline()
End Sub
Shared Sub ThreadProc()
Dim name As String = Thread.CurrentThread.Name
Console.WriteLine("{0} waits on AutoResetEvent #1.", name)
event_1.WaitOne()
Console.WriteLine("{0} is released from AutoResetEvent #1.", name)
Console.WriteLine("{0} waits on AutoResetEvent #2.", name)
event_2.WaitOne()
Console.WriteLine("{0} is released from AutoResetEvent #2.", name)
Console.WriteLine("{0} ends.", name)
End Sub
End Class
' This example produces output similar to the following:
'
'Press Enter to create three threads and start them.
'The threads wait on AutoResetEvent #1, which was created
'in the signaled state, so the first thread is released.
'This puts AutoResetEvent #1 into the unsignaled state.
'
'Thread_1 waits on AutoResetEvent #1.
'Thread_1 is released from AutoResetEvent #1.
'Thread_1 waits on AutoResetEvent #2.
'Thread_3 waits on AutoResetEvent #1.
'Thread_2 waits on AutoResetEvent #1.
'Press Enter to release another thread.
'
'Thread_3 is released from AutoResetEvent #1.
'Thread_3 waits on AutoResetEvent #2.
'Press Enter to release another thread.
'
'Thread_2 is released from AutoResetEvent #1.
'Thread_2 waits on AutoResetEvent #2.
'
'All threads are now waiting on AutoResetEvent #2.
'Press Enter to release a thread.
'
'Thread_2 is released from AutoResetEvent #2.
'Thread_2 ends.
'Press Enter to release a thread.
'
'Thread_1 is released from AutoResetEvent #2.
'Thread_1 ends.
'Press Enter to release a thread.
'
'Thread_3 is released from AutoResetEvent #2.
'Thread_3 ends.
注解
将 AutoResetEvent
、 ManualResetEvent和 EventWaitHandle 用于线程交互 (或线程信号) 。 有关详细信息,请参阅 线程交互。
线程通过调用 AutoResetEvent.WaitOne 来等待信号。 AutoResetEvent
如果 处于非信号状态,线程将阻塞,直到调用 AutoResetEvent.Set。 调用 Set
信号 AutoResetEvent
以释放等待的线程。 AutoResetEvent
在调用 或释放单个等待线程之前 Reset
保持信号,此时它会自动返回到非信号状态。
如果 进入信号状态时 AutoResetEvent
没有线程正在等待,则状态将保持信号状态,直到线程通过调用 WaitOne) 观察信号 (。 该线程不会阻止:立即 AutoResetEvent 释放线程并返回到非信号状态。
重要
不能保证每次调用 Set 方法都会释放线程。 如果两个调用在一起太近,导致第二次调用在线程释放之前发生,则只释放一个线程。 就好像第二次调用没有发生。 此外,如果在 Set 没有线程等待且 AutoResetEvent 已发出 信号时调用 ,则调用无效。
可以通过将布尔值传递给构造函数来控制 的初始状态 AutoResetEvent
: true
如果初始状态已发出 false
信号,则为 ;否则为 。
AutoResetEvent
还可以与 和 WaitAny 方法一static
WaitAll起使用。
AutoResetEvent 派生自 EventWaitHandle 类。 在 AutoResetEvent 功能上等效于 EventWaitHandle 使用 EventResetMode.AutoReset创建的 。
注意
AutoResetEvent与 类不同, EventWaitHandle 类提供对命名系统同步事件的访问权限。
重要
此类型实现 IDisposable 接口。 在使用完类型后,您应直接或间接释放类型。 若要直接释放类型,请在 try
/catch
块中调用其 Dispose 方法。 若要间接释放类型,请使用 using
(在 C# 中)或 Using
(在 Visual Basic 中)等语言构造。 有关详细信息,请参阅接口页上的“使用实现 IDisposable 的对象”部分 IDisposable 。
构造函数
AutoResetEvent(Boolean) |
用一个指示是否将初始状态设置为终止的布尔值初始化 AutoResetEvent 类的新实例。 |
字段
WaitTimeout |
指示在任何等待句柄终止之前 WaitAny(WaitHandle[], Int32, Boolean) 操作已超时。 此字段为常数。 (继承自 WaitHandle) |
属性
Handle |
已过时.
已过时.
获取或设置本机操作系统句柄。 (继承自 WaitHandle) |
SafeWaitHandle |
获取或设置本机操作系统句柄。 (继承自 WaitHandle) |
方法
Close() |
释放由当前 WaitHandle 占用的所有资源。 (继承自 WaitHandle) |
CreateObjRef(Type) |
创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。 (继承自 MarshalByRefObject) |
Dispose() |
释放 WaitHandle 类的当前实例所使用的所有资源。 (继承自 WaitHandle) |
Dispose(Boolean) |
当在派生类中重写时,释放 WaitHandle 使用的非托管资源,并且可选择释放托管资源。 (继承自 WaitHandle) |
Equals(Object) |
确定指定对象是否等于当前对象。 (继承自 Object) |
GetAccessControl() |
获取 EventWaitHandleSecurity 对象,该对象表示由当前 EventWaitHandle 对象表示的已命名系统事件的访问控制安全性。 (继承自 EventWaitHandle) |
GetHashCode() |
作为默认哈希函数。 (继承自 Object) |
GetLifetimeService() |
已过时.
检索控制此实例的生存期策略的当前生存期服务对象。 (继承自 MarshalByRefObject) |
GetType() |
获取当前实例的 Type。 (继承自 Object) |
InitializeLifetimeService() |
已过时.
获取生存期服务对象来控制此实例的生存期策略。 (继承自 MarshalByRefObject) |
MemberwiseClone() |
创建当前 Object 的浅表副本。 (继承自 Object) |
MemberwiseClone(Boolean) |
创建当前 MarshalByRefObject 对象的浅表副本。 (继承自 MarshalByRefObject) |
Reset() |
将事件状态设置为非终止,从而导致线程受阻。 |
Reset() |
将事件状态设置为非终止状态,导致线程阻止。 (继承自 EventWaitHandle) |
Set() |
将事件状态设置为终止,从而最多允许一个等待线程继续执行。 |
Set() |
将事件状态设置为终止状态,允许一个或多个等待线程继续。 (继承自 EventWaitHandle) |
SetAccessControl(EventWaitHandleSecurity) |
设置已命名的系统事件的访问控制安全性。 (继承自 EventWaitHandle) |
ToString() |
返回表示当前对象的字符串。 (继承自 Object) |
WaitOne() |
阻止当前线程,直到当前 WaitHandle 收到信号。 (继承自 WaitHandle) |
WaitOne(Int32) |
阻止当前线程,直到当前 WaitHandle 收到信号,同时使用 32 位带符号整数指定时间间隔(以毫秒为单位)。 (继承自 WaitHandle) |
WaitOne(Int32, Boolean) |
阻止当前线程,直到当前的 WaitHandle 收到信号为止,同时使用 32 位带符号整数指定时间间隔,并指定是否在等待之前退出同步域。 (继承自 WaitHandle) |
WaitOne(TimeSpan) |
阻止当前线程,直到当前实例收到信号,同时使用 TimeSpan 指定时间间隔。 (继承自 WaitHandle) |
WaitOne(TimeSpan, Boolean) |
阻止当前线程,直到当前实例收到信号为止,同时使用 TimeSpan 指定时间间隔,并指定是否在等待之前退出同步域。 (继承自 WaitHandle) |
显式接口实现
IDisposable.Dispose() |
此 API 支持产品基础结构,不能在代码中直接使用。 释放由 WaitHandle 使用的所有资源。 (继承自 WaitHandle) |
扩展方法
GetAccessControl(EventWaitHandle) |
返回指定的 |
SetAccessControl(EventWaitHandle, EventWaitHandleSecurity) |
设置指定事件等待句柄的安全描述符。 |
GetSafeWaitHandle(WaitHandle) |
获取本机操作系统等待句柄的安全句柄。 |
SetSafeWaitHandle(WaitHandle, SafeWaitHandle) |
设置本机操作系统等待句柄的安全句柄。 |
适用于
线程安全性
此类是线程安全的。
另请参阅
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈