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 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
也可以搭配 static
WaitAll 和 WaitAny 方法使用。
AutoResetEvent 衍生自 EventWaitHandle 類別。 AutoResetEvent的功能相當於使用 EventResetMode.AutoReset所建立的 EventWaitHandle 。
注意
AutoResetEvent不同於 類別,類別EventWaitHandle提供具名系統同步處理事件的存取權。
重要
此型別代表 IDisposable 介面。 當您完成使用型別時,您應該直接或間接處置它。 若要直接處置型別,請呼叫其 try
/catch
區塊中的 Dispose 方法。 若要間接處置它,請使用語言建構函式,例如 using
(在 C# 中) 或 Using
(在 Visual Basic 中)。 For more information, see the "Using an Object that Implements IDisposable" section on the IDisposable interface page.
建構函式
AutoResetEvent(Boolean) |
使用布林值 (Boolean) 來初始化 AutoResetEvent 類別的新執行個體,指出初始狀態是否設定為信號狀態。 |
欄位
WaitTimeout |
表示 WaitAny(WaitHandle[], Int32, Boolean) 作業在發出任何等候控制代碼信號之前便已逾時。 這個欄位為常數。 (繼承來源 WaitHandle) |
屬性
Handle |
已淘汰.
已淘汰.
取得或設定原生 (Native) 的作業系統控制代碼。 (繼承來源 WaitHandle) |
SafeWaitHandle |
取得或設定原生 (Native) 的作業系統控制代碼。 (繼承來源 WaitHandle) |
方法
Close() |
釋放目前 WaitHandle 所持有的全部資源。 (繼承來源 WaitHandle) |
CreateObjRef(Type) |
建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。 (繼承來源 MarshalByRefObject) |
Dispose() |
釋放 WaitHandle 類別目前的執行個體所使用的全部資源。 (繼承來源 WaitHandle) |
Dispose(Boolean) |
當在衍生類別中覆寫時,釋放 WaitHandle 所使用的 Unmanaged 資源,並選擇性釋放 Managed 資源。 (繼承來源 WaitHandle) |
Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
GetAccessControl() |
取得 EventWaitHandleSecurity 物件,此物件代表由目前 EventWaitHandle 物件所表示的具名系統事件的存取控制安全性。 (繼承來源 EventWaitHandle) |
GetHashCode() |
做為預設雜湊函式。 (繼承來源 Object) |
GetLifetimeService() |
已淘汰.
擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。 (繼承來源 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 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應