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 。 範例以 a ManualResetEvent 處於無訊號狀態(即 false 傳遞給建構者)開始。 範例中會產生三個執行緒,每個執行緒透過呼叫其WaitOne方法來阻擋 。ManualResetEvent 當使用者按下 Enter 鍵時,範例會呼叫該 Set 方法,釋放三個執行緒。 與此 AutoResetEvent 形成對比的是,類別會一次釋放一個執行緒,並在每次釋放後自動重置。

再次按下 Enter 鍵會顯示該 ManualResetEvent 執行緒保持在訊號狀態,直到 Reset 其方法被呼叫:範例啟動了另外兩個執行緒。 這些執行緒在呼叫 WaitOne 方法時不會阻塞,而是執行至完成。

再次按下 Enter 鍵會讓範例呼叫該 Reset 方法並啟動另一個執行緒,當呼叫 WaitOne時執行緒會阻塞。 最後一次按 Enter 鍵會釋放 Set 最後執行緒,程式即結束。

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.

備註

你會使用 ManualResetEventAutoResetEvent、 來 EventWaitHandle 進行線程互動(或線程訊號)。 欲了解更多資訊,請參閱《同步原語概述》文章中的線程互動或訊號部分

當執行緒開始必須完成的活動,其他執行緒才能繼續時,會呼叫 ManualResetEvent.Reset 以進入 ManualResetEvent 非訊號狀態。 此線程可視為控制 ManualResetEvent。 呼叫 ManualResetEvent.WaitOne 區塊的執行緒,正在等待訊號。 當控制執行緒完成該活動時,會呼叫 ManualResetEvent.Set 來表示等待的執行緒可以繼續。 所有等候的線程都會釋放。

一旦被通知,就會 ManualResetEvent 保持訊號狀態,直到手動呼叫 Reset() 方法重置為止。 也就是說,請立即回來 WaitOne

你可以透過將布林值傳給建構子來控制 a ManualResetEvent 的初始狀態: true 如果初始狀態被標示,否則 false

ManualResetEvent 也可以搭配 和 staticWaitAllWaitAny 方法一起使用。

從 .NET Framework 2.0 版本開始,從 ManualResetEvent 衍生自 EventWaitHandle 類別。 A ManualResetEvent 在功能上等價於由 創造的 EventWaitHandleEventResetMode.ManualReset

注意

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

從 .NET Framework 4.0 版本開始,System.Threading.ManualResetEventSlim 類別成為 ManualResetEvent 的輕量替代品。

建構函式

名稱 Description
ManualResetEvent(Boolean)

初始化一個新的類別實例 ManualResetEvent ,並以布林值表示是否將初始狀態設為 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)

回傳指定 handle的安全描述符。

GetSafeWaitHandle(WaitHandle)

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

SetAccessControl(EventWaitHandle, EventWaitHandleSecurity)

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

SetSafeWaitHandle(WaitHandle, SafeWaitHandle)

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

適用於

執行緒安全性

這個類別是安全線程。

另請參閱