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
- 继承
- 继承
- 继承
- 属性
示例
以下示例演示如何一AutoResetEvent次释放一个线程,方法是每次用户按 Enter 键时调用Set方法(基类)。 该示例启动三个 AutoResetEvent 线程,等待处于信号状态创建的线程。 第一个线程会立即释放,因为该 AutoResetEvent 线程已处于信号状态。 这会重置 AutoResetEvent 为非信号状态,以便后续线程阻止。 直到用户通过按 Enter 键一次释放一个线程,才会释放被阻止的线程。
从第一个 AutoResetEvent线程释放后,它们等待另一个 AutoResetEvent 处于非信号状态创建的线程。 所有三个线程都块,因此 Set 必须调用该方法三次才能释放所有线程。
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还可以与和方法一起使用staticWaitAll。WaitAny
AutoResetEvent 派生自 EventWaitHandle 类。 在 AutoResetEvent 功能上等效于 EventWaitHandle 使用 EventResetMode.AutoReset.
注意
与类 AutoResetEvent 不同,该 EventWaitHandle 类提供对命名系统同步事件的访问权限。
重要
此类型实现 IDisposable 接口。 使用完该类型后,应直接或间接释放它。 若要直接释放类型,请在块中Disposetry/调用其catch方法。 若要间接释放它,请使用语言构造,例如 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, Boolean) |
阻止当前线程,直到当前 WaitHandle 接收信号,使用 32 位有符号整数指定时间间隔,并指定是否在等待之前退出同步域。 (继承自 WaitHandle) |
| WaitOne(Int32) |
阻止当前线程,直到当前 WaitHandle 接收信号,使用 32 位有符号整数指定时间间隔(以毫秒为单位)。 (继承自 WaitHandle) |
| WaitOne(TimeSpan, Boolean) |
阻止当前线程,直到当前实例收到信号,使用指定 TimeSpan 时间间隔并指定是否在等待之前退出同步域。 (继承自 WaitHandle) |
| WaitOne(TimeSpan) |
阻止当前线程,直到当前实例收到信号,并使用 TimeSpan 指定时间间隔。 (继承自 WaitHandle) |
显式接口实现
| 名称 | 说明 |
|---|---|
| IDisposable.Dispose() |
此 API 支持产品基础结构,不能在代码中直接使用。 释放该 WaitHandle命令使用的所有资源。 (继承自 WaitHandle) |
扩展方法
| 名称 | 说明 |
|---|---|
| GetAccessControl(EventWaitHandle) |
返回指定 |
| GetSafeWaitHandle(WaitHandle) |
获取本机操作系统等待句柄的安全句柄。 |
| SetAccessControl(EventWaitHandle, EventWaitHandleSecurity) |
设置指定事件等待句柄的安全描述符。 |
| SetSafeWaitHandle(WaitHandle, SafeWaitHandle) |
设置本机操作系统等待句柄的安全句柄。 |
适用于
线程安全性
此类是线程安全的。