ManualResetEvent 類別

定義

表示執行緒同步處理事件,收到訊號時,必須手動重設。Represents a thread synchronization event that, when signaled, must be reset manually. 無法繼承這個類別。This class cannot be inherited.

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
Public NotInheritable Class ManualResetEvent
Inherits EventWaitHandle
Public NotInheritable Class ManualResetEvent
Inherits WaitHandle
繼承
ManualResetEvent
繼承
繼承
屬性

範例

下列範例示範 ManualResetEvent 的運作方式。The following example demonstrates how ManualResetEvent works. 此範例會從未收到訊號狀態中的 ManualResetEvent 開始(也就是 false 會傳遞給此函式)。The example starts with a ManualResetEvent in the unsignaled state (that is, false is passed to the constructor). 此範例會建立三個執行緒,其中每一個都是藉由呼叫其 WaitOne 方法來封鎖 ManualResetEventThe example creates three threads, each of which blocks on the ManualResetEvent by calling its WaitOne method. 當使用者按下Enter鍵時,範例會呼叫 Set 方法,這會釋放這三個執行緒。When the user presses the Enter key, the example calls the Set method, which releases all three threads. 這與 AutoResetEvent 類別的行為相反,它會一次釋放一個執行緒,並在每次發行之後自動重設。Contrast this with the behavior of the AutoResetEvent class, which releases threads one at a time, resetting automatically after each release.

再次按下Enter鍵,會顯示 ManualResetEvent 維持在已發出信號的狀態,直到呼叫其 Reset 方法為止:此範例會啟動兩個以上的執行緒。Pressing the Enter key again demonstrates that the ManualResetEvent remains in the signaled state until its Reset method is called: The example starts two more threads. 這些執行緒在呼叫 WaitOne 方法時不會封鎖,而是執行至完成。These threads do not block when they call the WaitOne method, but instead run to completion.

再次按下Enter鍵會導致範例呼叫 Reset 方法,並啟動一個以上的執行緒,以在呼叫 WaitOne時封鎖。Pressing the Enter key again causes the example to call the Reset method and to start one more thread, which blocks when it calls WaitOne. enter鍵,最後一次呼叫 Set 以釋放最後一個執行緒,程式結束。Pressing the Enter key one final time calls Set to release the last thread, and the program ends.

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 進行執行緒互動(或執行緒信號)。You use ManualResetEvent, AutoResetEvent, and EventWaitHandle for thread interaction (or thread signaling). 如需詳細資訊,請參閱同步處理原始物件的總覽一文中的執行緒互動或信號一節。For more information, see the Thread interaction, or signaling section of the Overview of synchronization primitives article.

當執行緒開始必須在其他執行緒繼續執行之前完成的活動時,它會呼叫ManualResetEvent ,使 ManualResetEvent 處於非信號狀態。When a thread begins an activity that must complete before other threads proceed, it calls ManualResetEvent.Reset to put ManualResetEvent in the non-signaled state. 您可以將這個執行緒視為控制 ManualResetEventThis thread can be thought of as controlling the ManualResetEvent. 呼叫ManualResetEvent WaitOne封鎖的執行緒,等候信號。Threads that call ManualResetEvent.WaitOne block, awaiting the signal. 當控制執行緒完成活動時,它會呼叫ManualResetEvent。設定以表示等候中的執行緒可以繼續。When the controlling thread completes the activity, it calls ManualResetEvent.Set to signal that the waiting threads can proceed. 所有等候中的執行緒都會釋放。All waiting threads are released.

一旦收到信號,ManualResetEvent 會保持信號,直到藉由呼叫 Reset() 方法來手動重設為止。Once it has been signaled, ManualResetEvent remains signaled until it is manually reset by calling the Reset() method. 也就是說,WaitOne 的呼叫會立即傳回。That is, calls to WaitOne return immediately.

您可以控制 ManualResetEvent 的初始狀態,方法是將布林值傳遞給此函式:如果初始狀態是已信號,則為 true,否則為 falseYou can control the initial state of a ManualResetEvent by passing a Boolean value to the constructor: true if the initial state is signaled, and false otherwise.

ManualResetEvent 也可以與 static WaitAllWaitAny 方法搭配使用。ManualResetEvent can also be used with the static WaitAll and WaitAny methods.

從 .NET Framework 版本2.0 開始,ManualResetEvent 衍生自 EventWaitHandle 類別。Beginning with the .NET Framework version 2.0, ManualResetEvent derives from the EventWaitHandle class. ManualResetEvent 在功能上相當於使用 EventResetMode.ManualReset建立的 EventWaitHandleA ManualResetEvent is functionally equivalent to an EventWaitHandle created with EventResetMode.ManualReset.

注意

不同于 ManualResetEvent 類別,EventWaitHandle 類別可讓您存取已命名的系統同步處理事件。Unlike the ManualResetEvent class, the EventWaitHandle class provides access to named system synchronization events.

從 .NET Framework 版本4.0 開始,System.Threading.ManualResetEventSlim 類別是 ManualResetEvent的輕量替代方式。Beginning with the .NET Framework version 4.0, the System.Threading.ManualResetEventSlim class is a lightweight alternative to ManualResetEvent.

建構函式

ManualResetEvent(Boolean)

使用布林值 (Boolean) 來初始化 ManualResetEvent 類別的新執行個體,指出初始狀態是否設定為信號狀態。Initializes a new instance of the ManualResetEvent class with a Boolean value indicating whether to set the initial state to signaled.

欄位

WaitTimeout

表示 WaitAny(WaitHandle[], Int32, Boolean) 作業在發出任何等候控制代碼信號之前便已逾時。Indicates that a WaitAny(WaitHandle[], Int32, Boolean) operation timed out before any of the wait handles were signaled. 這個欄位是常數。This field is constant.

(繼承來源 WaitHandle)

屬性

Handle

取得或設定原生 (Native) 的作業系統控制代碼。Gets or sets the native operating system handle.

(繼承來源 WaitHandle)
SafeWaitHandle

取得或設定原生 (Native) 的作業系統控制代碼。Gets or sets the native operating system handle.

(繼承來源 WaitHandle)

方法

Close()

釋放目前 WaitHandle 所持有的全部資源。Releases all resources held by the current WaitHandle.

(繼承來源 WaitHandle)
CreateObjRef(Type)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(繼承來源 MarshalByRefObject)
Dispose()

釋放目前的 WaitHandle 類別執行個體所使用的所有資源。Releases all resources used by the current instance of the WaitHandle class.

(繼承來源 WaitHandle)
Dispose(Boolean)

當在衍生類別中覆寫時,釋放 WaitHandle 所使用的 Unmanaged 資源,並選擇性釋放 Managed 資源。When overridden in a derived class, releases the unmanaged resources used by the WaitHandle, and optionally releases the managed resources.

(繼承來源 WaitHandle)
Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetAccessControl()

取得 EventWaitHandleSecurity 物件,表示目前 EventWaitHandle 物件代表之具名系統事件的存取控制安全性。Gets an EventWaitHandleSecurity object that represents the access control security for the named system event represented by the current EventWaitHandle object.

(繼承來源 EventWaitHandle)
GetHashCode()

作為預設雜湊函數。Serves as the default hash function.

(繼承來源 Object)
GetLifetimeService()

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(繼承來源 MarshalByRefObject)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
InitializeLifetimeService()

取得存留期服務物件,以控制這個執行個體的存留期原則。Obtains a lifetime service object to control the lifetime policy for this instance.

(繼承來源 MarshalByRefObject)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
MemberwiseClone(Boolean)

建立目前 MarshalByRefObject 物件的淺層複本。Creates a shallow copy of the current MarshalByRefObject object.

(繼承來源 MarshalByRefObject)
Reset()

將事件的狀態設定為未收到信號,會造成執行緒封鎖。Sets the state of the event to nonsignaled, which causes threads to block.

Set()

將事件的狀態設定為未收到信號,讓一個或多個等候執行緒繼續執行。Sets the state of the event to signaled, which allows one or more waiting threads to proceed.

SetAccessControl(EventWaitHandleSecurity)

為具名系統事件設定存取控制安全性。Sets the access control security for a named system event.

(繼承來源 EventWaitHandle)
ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)
WaitOne()

封鎖目前的執行緒,直到目前的 WaitHandle 收到訊號為止。Blocks the current thread until the current WaitHandle receives a signal.

(繼承來源 WaitHandle)
WaitOne(Int32)

封鎖目前執行緒,直到目前的 WaitHandle 收到信號為止,使用 32 位元帶正負號的整數來指定時間間隔 (以毫秒為單位)。Blocks the current thread until the current WaitHandle receives a signal, using a 32-bit signed integer to specify the time interval in milliseconds.

(繼承來源 WaitHandle)
WaitOne(Int32, Boolean)

封鎖目前執行緒,直到目前的 WaitHandle 收到信號為止,使用 32 位元帶正負號的整數來指定時間間隔,並指定是否要先離開同步處理網域,再開始等候。Blocks the current thread until the current WaitHandle receives a signal, using a 32-bit signed integer to specify the time interval and specifying whether to exit the synchronization domain before the wait.

(繼承來源 WaitHandle)
WaitOne(TimeSpan)

封鎖目前執行緒,直到目前執行個體收到信號為止,使用 TimeSpan 來指定時間間隔。Blocks the current thread until the current instance receives a signal, using a TimeSpan to specify the time interval.

(繼承來源 WaitHandle)
WaitOne(TimeSpan, Boolean)

封鎖目前執行緒,直到目前執行個體收到信號為止,使用 TimeSpan 來指定時間間隔,並指定是否要先離開同步處理網域,再開始等候。Blocks the current thread until the current instance receives a signal, using a TimeSpan to specify the time interval and specifying whether to exit the synchronization domain before the wait.

(繼承來源 WaitHandle)

明確介面實作

IDisposable.Dispose()

釋放 WaitHandle 使用的所有資源。Releases all resources used by the WaitHandle.

(繼承來源 WaitHandle)

擴充方法

GetAccessControl(EventWaitHandle)
SetAccessControl(EventWaitHandle, EventWaitHandleSecurity)
GetSafeWaitHandle(WaitHandle)

取得原生作業系統等候控制代碼的安全控制代碼。Gets the safe handle for a native operating system wait handle.

SetSafeWaitHandle(WaitHandle, SafeWaitHandle)

設定原生作業系統等候控制代碼的安全控制代碼。Sets a safe handle for a native operating system wait handle.

適用於

執行緒安全性

這個類別是安全線程。This class is thread safe.

另請參閱