共用方式為


ManualResetEvent 類別

定義

表示必須手動重設訊號的線程同步處理事件。 無法繼承這個類別。

public ref class ManualResetEvent sealed : System::Threading::EventWaitHandle
public ref class ManualResetEvent sealed : System::Threading::WaitHandle
public sealed class ManualResetEvent : System.Threading.EventWaitHandle
public sealed class ManualResetEvent : System.Threading.WaitHandle
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class ManualResetEvent : System.Threading.EventWaitHandle
type ManualResetEvent = class
    inherit EventWaitHandle
type ManualResetEvent = class
    inherit WaitHandle
[<System.Runtime.InteropServices.ComVisible(true)>]
type ManualResetEvent = class
    inherit EventWaitHandle
Public NotInheritable Class ManualResetEvent
Inherits EventWaitHandle
Public NotInheritable Class ManualResetEvent
Inherits WaitHandle
繼承
ManualResetEvent
繼承
繼承
屬性

範例

下列範例示範 ManualResetEvent 的運作方式。 此範例會以處於未對齊狀態的 ManualResetEvent 開頭(也就是,false 傳遞至建構函式)。 此範例會建立三個線程,每個線程都會藉由呼叫其 WaitOne 方法,在 ManualResetEvent 上封鎖。 當使用者按下 Enter 鍵時,此範例會呼叫 Set 方法,以釋放這三個線程。 這與 AutoResetEvent 類別的行為形成對比,該類別會一次釋放線程一次,在每個發行之後自動重設。

再次按下 Enter 鍵,會再次示範 ManualResetEvent 會保持信號狀態,直到呼叫其 Reset 方法為止:此範例會再啟動兩個線程。 這些線程不會在呼叫 WaitOne 方法時封鎖,而是改為執行至完成。

再次按 Enter 鍵會導致範例呼叫 Reset 方法並啟動一個線程,這會在呼叫 WaitOne時封鎖 。 按 Enter 鍵 鍵最後一次呼叫 Set 釋放最後一個線程,程序結束。

using namespace System;
using namespace System::Threading;

ref class Example
{
private:
    // mre is used to block and release threads manually. It is
    // created in the unsignaled state.
    static ManualResetEvent^ mre = gcnew ManualResetEvent(false);

    static void ThreadProc()
    {
        String^ name = Thread::CurrentThread->Name;

        Console::WriteLine(name + " starts and calls mre->WaitOne()");

        mre->WaitOne();

        Console::WriteLine(name + " ends.");
    }

public:
    static void Demo()
    {
        Console::WriteLine("\nStart 3 named threads that block on a ManualResetEvent:\n");

        for(int i = 0; i <=2 ; i++)
        {
            Thread^ t = gcnew Thread(gcnew ThreadStart(ThreadProc));
            t->Name = "Thread_" + i;
            t->Start();
        }

        Thread::Sleep(500);
        Console::WriteLine("\nWhen all three threads have started, press Enter to call Set()" +
                           "\nto release all the threads.\n");
        Console::ReadLine();

        mre->Set();

        Thread::Sleep(500);
        Console::WriteLine("\nWhen a ManualResetEvent is signaled, threads that call WaitOne()" +
                           "\ndo not block. Press Enter to show this.\n");
        Console::ReadLine();

        for(int i = 3; i <= 4; i++)
        {
            Thread^ t = gcnew Thread(gcnew ThreadStart(ThreadProc));
            t->Name = "Thread_" + i;
            t->Start();
        }

        Thread::Sleep(500);
        Console::WriteLine("\nPress Enter to call Reset(), so that threads once again block" +
                           "\nwhen they call WaitOne().\n");
        Console::ReadLine();

        mre->Reset();

        // Start a thread that waits on the ManualResetEvent.
        Thread^ t5 = gcnew Thread(gcnew ThreadStart(ThreadProc));
        t5->Name = "Thread_5";
        t5->Start();

        Thread::Sleep(500);
        Console::WriteLine("\nPress Enter to call Set() and conclude the demo.");
        Console::ReadLine();

        mre->Set();

        // If you run this example in Visual Studio, uncomment the following line:
        //Console::ReadLine();
    }
};

int main()
{
   Example::Demo();
}

/* This example produces output similar to the following:

Start 3 named threads that block on a ManualResetEvent:

Thread_0 starts and calls mre->WaitOne()
Thread_1 starts and calls mre->WaitOne()
Thread_2 starts and calls mre->WaitOne()

When all three threads have started, press Enter to call Set()
to release all the threads.


Thread_2 ends.
Thread_1 ends.
Thread_0 ends.

When a ManualResetEvent is signaled, threads that call WaitOne()
do not block. Press Enter to show this.


Thread_3 starts and calls mre->WaitOne()
Thread_3 ends.
Thread_4 starts and calls mre->WaitOne()
Thread_4 ends.

Press Enter to call Reset(), so that threads once again block
when they call WaitOne().


Thread_5 starts and calls mre->WaitOne()

Press Enter to call Set() and conclude the demo.

Thread_5 ends.
 */
using System;
using System.Threading;

public class Example
{
    // mre is used to block and release threads manually. It is
    // created in the unsignaled state.
    private static ManualResetEvent mre = new ManualResetEvent(false);

    static void Main()
    {
        Console.WriteLine("\nStart 3 named threads that block on a ManualResetEvent:\n");

        for(int i = 0; i <= 2; i++)
        {
            Thread t = new Thread(ThreadProc);
            t.Name = "Thread_" + i;
            t.Start();
        }

        Thread.Sleep(500);
        Console.WriteLine("\nWhen all three threads have started, press Enter to call Set()" +
                          "\nto release all the threads.\n");
        Console.ReadLine();

        mre.Set();

        Thread.Sleep(500);
        Console.WriteLine("\nWhen a ManualResetEvent is signaled, threads that call WaitOne()" +
                          "\ndo not block. Press Enter to show this.\n");
        Console.ReadLine();

        for(int i = 3; i <= 4; i++)
        {
            Thread t = new Thread(ThreadProc);
            t.Name = "Thread_" + i;
            t.Start();
        }

        Thread.Sleep(500);
        Console.WriteLine("\nPress Enter to call Reset(), so that threads once again block" +
                          "\nwhen they call WaitOne().\n");
        Console.ReadLine();

        mre.Reset();

        // Start a thread that waits on the ManualResetEvent.
        Thread t5 = new Thread(ThreadProc);
        t5.Name = "Thread_5";
        t5.Start();

        Thread.Sleep(500);
        Console.WriteLine("\nPress Enter to call Set() and conclude the demo.");
        Console.ReadLine();

        mre.Set();

        // If you run this example in Visual Studio, uncomment the following line:
        //Console.ReadLine();
    }

    private static void ThreadProc()
    {
        string name = Thread.CurrentThread.Name;

        Console.WriteLine(name + " starts and calls mre.WaitOne()");

        mre.WaitOne();

        Console.WriteLine(name + " ends.");
    }
}

/* This example produces output similar to the following:

Start 3 named threads that block on a ManualResetEvent:

Thread_0 starts and calls mre.WaitOne()
Thread_1 starts and calls mre.WaitOne()
Thread_2 starts and calls mre.WaitOne()

When all three threads have started, press Enter to call Set()
to release all the threads.


Thread_2 ends.
Thread_0 ends.
Thread_1 ends.

When a ManualResetEvent is signaled, threads that call WaitOne()
do not block. Press Enter to show this.


Thread_3 starts and calls mre.WaitOne()
Thread_3 ends.
Thread_4 starts and calls mre.WaitOne()
Thread_4 ends.

Press Enter to call Reset(), so that threads once again block
when they call WaitOne().


Thread_5 starts and calls mre.WaitOne()

Press Enter to call Set() and conclude the demo.

Thread_5 ends.
 */
Imports System.Threading

Public Class Example

    ' mre is used to block and release threads manually. It is
    ' created in the unsignaled state.
    Private Shared mre As New ManualResetEvent(False)

    <MTAThreadAttribute> _
    Shared Sub Main()

        Console.WriteLine(vbLf & _
            "Start 3 named threads that block on a ManualResetEvent:" & vbLf)

        For i As Integer = 0 To 2
            Dim t As New Thread(AddressOf ThreadProc)
            t.Name = "Thread_" & i
            t.Start()
        Next i

        Thread.Sleep(500)
        Console.WriteLine(vbLf & _
            "When all three threads have started, press Enter to call Set()" & vbLf & _
            "to release all the threads." & vbLf)
        Console.ReadLine()

        mre.Set()

        Thread.Sleep(500)
        Console.WriteLine(vbLf & _
            "When a ManualResetEvent is signaled, threads that call WaitOne()" & vbLf & _
            "do not block. Press Enter to show this." & vbLf)
        Console.ReadLine()

        For i As Integer = 3 To 4
            Dim t As New Thread(AddressOf ThreadProc)
            t.Name = "Thread_" & i
            t.Start()
        Next i

        Thread.Sleep(500)
        Console.WriteLine(vbLf & _
            "Press Enter to call Reset(), so that threads once again block" & vbLf & _
            "when they call WaitOne()." & vbLf)
        Console.ReadLine()

        mre.Reset()

        ' Start a thread that waits on the ManualResetEvent.
        Dim t5 As New Thread(AddressOf ThreadProc)
        t5.Name = "Thread_5"
        t5.Start()

        Thread.Sleep(500)
        Console.WriteLine(vbLf & "Press Enter to call Set() and conclude the demo.")
        Console.ReadLine()

        mre.Set()

        ' If you run this example in Visual Studio, uncomment the following line:
        'Console.ReadLine()

    End Sub


    Private Shared Sub ThreadProc()

        Dim name As String = Thread.CurrentThread.Name

        Console.WriteLine(name & " starts and calls mre.WaitOne()")

        mre.WaitOne()

        Console.WriteLine(name & " ends.")

    End Sub

End Class

' This example produces output similar to the following:
'
'Start 3 named threads that block on a ManualResetEvent:
'
'Thread_0 starts and calls mre.WaitOne()
'Thread_1 starts and calls mre.WaitOne()
'Thread_2 starts and calls mre.WaitOne()
'
'When all three threads have started, press Enter to call Set()
'to release all the threads.
'
'
'Thread_2 ends.
'Thread_0 ends.
'Thread_1 ends.
'
'When a ManualResetEvent is signaled, threads that call WaitOne()
'do not block. Press Enter to show this.
'
'
'Thread_3 starts and calls mre.WaitOne()
'Thread_3 ends.
'Thread_4 starts and calls mre.WaitOne()
'Thread_4 ends.
'
'Press Enter to call Reset(), so that threads once again block
'when they call WaitOne().
'
'
'Thread_5 starts and calls mre.WaitOne()
'
'Press Enter to call Set() and conclude the demo.
'
'Thread_5 ends.

備註

您可以使用 ManualResetEventAutoResetEventEventWaitHandle 進行線程互動(或線程訊號)。 如需詳細資訊,請參閱 同步處理 基本概觀一文的 線程互動或發出訊號 一節。

當線程開始必須在其他線程繼續之前完成的活動時,它會呼叫 manualResetEvent.Reset,以將 ManualResetEvent 置於非訊號狀態。 此線程可視為控制 ManualResetEvent。 呼叫 ManualResetEvent.WaitOne 區塊的線程,等待訊號。 當控制線程完成活動時,它會呼叫 ManualResetEvent.Set,以發出等候線程可以繼續的訊號。 所有等候的線程都會釋放。

一旦發出訊號,ManualResetEvent 會保持訊號,直到呼叫 Reset() 方法來手動重設為止。 也就是說,呼叫 WaitOne 會立即傳回。

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

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

從 .NET Framework 2.0 版開始,ManualResetEvent 衍生自 EventWaitHandle 類別。 ManualResetEvent 的功能相當於使用 EventResetMode.ManualReset建立的 EventWaitHandle

注意

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

從 .NET Framework 4.0 版開始,System.Threading.ManualResetEventSlim 類別是 ManualResetEvent的輕量型替代方案。

建構函式

ManualResetEvent(Boolean)

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

欄位

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)

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

適用於

執行緒安全性

這個類別是安全線程。

另請參閱