共用方式為


AutoResetEvent 類別

定義

表示線程同步處理事件,當發出訊號時,釋放一個等候中的線程,然後自動重設。 無法繼承這個類別。

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.

備註

您可以使用 AutoResetEventManualResetEventEventWaitHandle 進行線程互動(或線程訊號)。 如需詳細資訊,請參閱 線程互動

線程會藉由呼叫 autoResetEvent.WaitOne 來等候訊號。 如果 AutoResetEvent 處於非訊號狀態,線程會封鎖直到呼叫 autoResetEvent.Set 為止。 呼叫 Set 訊號會 AutoResetEvent 釋放等候的線程。 AutoResetEvent 會保持訊號,直到呼叫 Reset 或釋放單一等候線程為止,此時它會自動回到非訊號狀態。

如果沒有線程在 AutoResetEvent 進入訊號狀態時等候,則狀態會保持訊號,直到線程觀察訊號為止(藉由呼叫 WaitOne)。 該線程不會封鎖:AutoResetEvent 會立即釋放線程,並返回非訊號狀態。

重要

不保證每次呼叫 Set 方法都會釋放線程。 如果兩個呼叫太接近,因此第二個呼叫會在釋放線程之前發生,則只會釋放一個線程。 就好像第二次呼叫沒有發生一樣。 此外,如果沒有等候的線程且 AutoResetEvent 已發出訊號時,就會呼叫 Set,則呼叫不會有任何作用。

您可以將布爾值傳遞至建構函式,以控制 AutoResetEvent 的初始狀態:如果初始狀態已發出訊號,則 true 否則 false

AutoResetEvent 也可以與 staticWaitAllWaitAny 方法搭配使用。

AutoResetEvent 衍生自 EventWaitHandle 類別。 AutoResetEvent 的功能相當於使用 EventResetMode.AutoReset建立的 EventWaitHandle

注意

不同於 AutoResetEvent 類別,EventWaitHandle 類別提供具名系統同步事件的存取權。

重要

此類型會實作 IDisposable 介面。 當您完成使用類型時,應該直接或間接處置它。 若要直接處置類型,請在 try/catch 區塊中呼叫其 Dispose 方法。 若要間接處置它,請使用語言建構,例如 using (C#) 或 Using (在 Visual Basic 中)。 如需詳細資訊,請參閱 介面頁面上的<使用實作 IDisposable 的物件>一節。

建構函式

AutoResetEvent(Boolean)

使用布爾值,初始化 AutoResetEvent 類別的新實例,指出是否要將初始狀態設定為已發出訊號。

欄位

WaitTimeout

表示 WaitAny(WaitHandle[], Int32, Boolean) 作業在發出任何等候句柄的訊號之前逾時。 此欄位是常數。

(繼承來源 WaitHandle)

屬性

Handle
已淘汰.
已淘汰.

取得或設定原生操作系統句柄。

(繼承來源 WaitHandle)
SafeWaitHandle

取得或設定原生操作系統句柄。

(繼承來源 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()
已淘汰.

擷取控制這個實例存留期原則的目前存留期服務物件。

(繼承來源 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)

封鎖目前的線程,直到目前 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)

傳回指定之 handle的安全性描述元。

SetAccessControl(EventWaitHandle, EventWaitHandleSecurity)

設定指定事件等候句柄的安全性描述項。

GetSafeWaitHandle(WaitHandle)

取得原生操作系統等候句柄的安全句柄。

SetSafeWaitHandle(WaitHandle, SafeWaitHandle)

設定原生操作系統等候句柄的安全句柄。

適用於

執行緒安全性

這個類別是安全線程。

另請參閱