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 如果初始狀態被 signed ,否則 false 。
AutoResetEvent 也可以搭配 和 staticWaitAllWaitAny 方法一起使用。
AutoResetEvent 由 EventWaitHandle 類別推導而來。 An AutoResetEvent 在功能上等 EventWaitHandle 價於 所 EventResetMode.AutoReset創造的 。
注意
與類別 AutoResetEvent 不同,類別 EventWaitHandle 提供命名系統同步事件的存取權限。
重要
此類型會實作 IDisposable 介面。 當您完成使用類型時,應該直接或間接處置它。 若要直接處置類型,請在 Disposetry/ 區塊中呼叫其 catch 方法。 若要間接處置它,請使用語言建構,例如 using (C#) 或 Using (在 Visual Basic 中)。 欲了解更多資訊,請參閱介面頁面上的 IDisposable 「使用實作 IDisposable 的物件」章節。
建構函式
| 名稱 | Description |
|---|---|
| AutoResetEvent(Boolean) |
初始化一個新的類別實例 AutoResetEvent ,並以布林值表示是否將初始狀態設為 signaled。 |
欄位
| 名稱 | Description |
|---|---|
| WaitTimeout |
表示操作 WaitAny(WaitHandle[], Int32, Boolean) 在任何等待句柄被通知前已逾時。 此欄位是常數。 (繼承來源 WaitHandle) |
屬性
| 名稱 | Description |
|---|---|
| Handle |
已淘汰.
已淘汰.
取得或設定原生操作系統句柄。 (繼承來源 WaitHandle) |
| SafeWaitHandle |
取得或設定原生操作系統句柄。 (繼承來源 WaitHandle) |
方法
| 名稱 | Description |
|---|---|
| Close() |
釋放目前 WaitHandle所持有的所有資源。 (繼承來源 WaitHandle) |
| CreateObjRef(Type) |
建立物件,其中包含產生用來與遠端物件通訊之 Proxy 所需的所有相關信息。 (繼承來源 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() |
將事件的狀態設定為非ignaled,這會導致線程封鎖。 |
| 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) |
阻塞目前執行緒直到目前實例收到訊號,並用 a TimeSpan 指定時間區間,並決定是否在等待前退出同步域。 (繼承來源 WaitHandle) |
| WaitOne(TimeSpan) |
阻塞目前執行緒直到目前實例收到訊號,並使用 a TimeSpan 指定時間區間。 (繼承來源 WaitHandle) |
明確介面實作
| 名稱 | Description |
|---|---|
| IDisposable.Dispose() |
此 API 支援此產品基礎結構,但無法直接用於程式碼之中。 釋放所有由 WaitHandle. (繼承來源 WaitHandle) |
擴充方法
| 名稱 | Description |
|---|---|
| GetAccessControl(EventWaitHandle) |
回傳指定 |
| GetSafeWaitHandle(WaitHandle) |
取得原生操作系統等候句柄的安全句柄。 |
| SetAccessControl(EventWaitHandle, EventWaitHandleSecurity) |
設定指定事件等候句柄的安全性描述項。 |
| SetSafeWaitHandle(WaitHandle, SafeWaitHandle) |
設定原生操作系統等候句柄的安全句柄。 |
適用於
執行緒安全性
這個類別是安全線程。
另請參閱
- WaitHandle
- 管理的執行緒
- 同步處理基本概觀