ManualResetEvent 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
表示執行緒同步處理事件,收到訊號時,必須手動重設。 此類別無法獲得繼承。
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 , 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.
備註
ManualResetEvent
您可以使用 、 AutoResetEvent 和 EventWaitHandle 來進行執行緒互動 (或執行緒訊號) 。 如需詳細資訊,請參閱同步處理基本概觀一文的執行緒互動或訊號一節。
當執行緒開始必須在其他執行緒繼續之前完成的活動時,它會呼叫 ManualResetEvent.Reset 以進入 ManualResetEvent
非訊號狀態。 此執行緒可視為控制 ManualResetEvent
。 呼叫 ManualResetEvent.WaitOne 區塊的執行緒,正在等候訊號。 當控制執行緒完成活動時,它會呼叫 ManualResetEvent.Set 以發出等候執行緒可以繼續的訊號。 所有等候中的執行緒都會釋出。
一旦收到訊號之後, ManualResetEvent
會保持訊號,直到呼叫 Reset() 方法手動重設為止。 也就是說,要立即傳回的 WaitOne 呼叫。
您可以藉由將布林值傳遞至建構函式來控制 的初始狀態 ManualResetEvent
: true
如果已發出初始狀態,則為 , false
否則為 。
ManualResetEvent
也可以搭配 static
WaitAll 和 WaitAny 方法使用。
從 .NET Framework 2.0 版開始, ManualResetEvent 衍生自 EventWaitHandle 類別。 ManualResetEvent的功能相當於 EventWaitHandle 使用 所建立的 EventResetMode.ManualReset 。
注意
不同于 類別 ManualResetEvent ,類別 EventWaitHandle 提供具名系統同步處理事件的存取權。
從 .NET Framework 4.0 版開始,類別 System.Threading.ManualResetEventSlim 是 的 ManualResetEvent 輕量型替代方案。
建構函式
ManualResetEvent(Boolean) |
使用布林值 (Boolean) 來初始化 ManualResetEvent 類別的新執行個體,指出初始狀態是否設定為信號狀態。 |
欄位
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) |
設定原生作業系統等候控制代碼的安全控制代碼。 |
適用於
執行緒安全性
這個類別是安全線程。
另請參閱
意見反應
提交並檢視相關的意見反應