EventWaitHandle 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 EventWaitHandle 類別的新執行個體。
多載
| 名稱 | Description |
|---|---|
| EventWaitHandle(Boolean, EventResetMode) |
初始化該類別的新實例 EventWaitHandle ,指定等待句柄是最初被標記,以及是自動或手動重置。 |
| EventWaitHandle(Boolean, EventResetMode, String) |
初始化該類別的新實例 EventWaitHandle ,指定若因呼叫而建立等待句柄,是否會先發出訊號,是自動或手動重置,以及系統同步事件名稱。 |
| EventWaitHandle(Boolean, EventResetMode, String, Boolean) |
初始化該類別的新實例 EventWaitHandle ,指定若因呼叫建立等待句柄是否會被最初發出訊號,是否自動或手動重置,系統同步事件名稱,以及呼叫後的布林變數值,指示該系統事件是否被建立。 |
| EventWaitHandle(Boolean, EventResetMode, String, NamedWaitHandleOptions) |
初始化該類別的新實例 EventWaitHandle ,指定若因呼叫建立等待句柄,是否會先發出訊號,是否會自動或手動重置,系統同步事件名稱,以及設定使用者範圍與會話範圍存取的選項。 |
| EventWaitHandle(Boolean, EventResetMode, String, Boolean, EventWaitHandleSecurity) |
初始化該類別的新實例 EventWaitHandle ,指定若因呼叫建立等待句柄,是否會先發出訊號,是否自動或手動重置,系統同步事件名稱,呼叫後的布林變數值表示該命名系統事件是否已建立,以及若該命名事件被建立,應套用的存取控制安全性。 |
| EventWaitHandle(Boolean, EventResetMode, String, NamedWaitHandleOptions, Boolean) |
初始化該類別的新實例 EventWaitHandle ,指定若因呼叫建立等待句柄,是否會先發出訊號,是否自動或手動重置,系統同步事件名稱,設定使用者範圍與會話範圍存取選項,以及呼叫後的布林變數,該變數值顯示該系統事件是否被建立。 |
EventWaitHandle(Boolean, EventResetMode)
初始化該類別的新實例 EventWaitHandle ,指定等待句柄是最初被標記,以及是自動或手動重置。
public:
EventWaitHandle(bool initialState, System::Threading::EventResetMode mode);
public EventWaitHandle(bool initialState, System.Threading.EventResetMode mode);
new System.Threading.EventWaitHandle : bool * System.Threading.EventResetMode -> System.Threading.EventWaitHandle
Public Sub New (initialState As Boolean, mode As EventResetMode)
參數
- initialState
- Boolean
true 將初始狀態設為已訊號; false 將它設為無訊號狀態。
- mode
- EventResetMode
EventResetMode這是決定事件是自動重置還是手動重置的數值之一。
例外狀況
mode列舉值超出法律範圍。
範例
以下程式碼範例使用 SignalAndWait(WaitHandle, WaitHandle) 方法過載,讓主執行緒能向阻塞的執行緒發出訊號,然後等待執行緒完成任務。
範例中會啟動五個執行緒,並允許它們在用EventWaitHandle旗標建立的執行EventResetMode.AutoReset緒上封鎖,然後每當使用者按下 Enter 鍵時,就會釋放一個執行緒。 範例接著排隊另外五個執行緒,並用 EventWaitHandle Created with the EventResetMode.ManualReset flag 釋放它們。
using System;
using System.Threading;
public class Example
{
// The EventWaitHandle used to demonstrate the difference
// between AutoReset and ManualReset synchronization events.
//
private static EventWaitHandle ewh;
// A counter to make sure all threads are started and
// blocked before any are released. A Long is used to show
// the use of the 64-bit Interlocked methods.
//
private static long threadCount = 0;
// An AutoReset event that allows the main thread to block
// until an exiting thread has decremented the count.
//
private static EventWaitHandle clearCount =
new EventWaitHandle(false, EventResetMode.AutoReset);
[MTAThread]
public static void Main()
{
// Create an AutoReset EventWaitHandle.
//
ewh = new EventWaitHandle(false, EventResetMode.AutoReset);
// Create and start five numbered threads. Use the
// ParameterizedThreadStart delegate, so the thread
// number can be passed as an argument to the Start
// method.
for (int i = 0; i <= 4; i++)
{
Thread t = new Thread(
new ParameterizedThreadStart(ThreadProc)
);
t.Start(i);
}
// Wait until all the threads have started and blocked.
// When multiple threads use a 64-bit value on a 32-bit
// system, you must access the value through the
// Interlocked class to guarantee thread safety.
//
while (Interlocked.Read(ref threadCount) < 5)
{
Thread.Sleep(500);
}
// Release one thread each time the user presses ENTER,
// until all threads have been released.
//
while (Interlocked.Read(ref threadCount) > 0)
{
Console.WriteLine("Press ENTER to release a waiting thread.");
Console.ReadLine();
// SignalAndWait signals the EventWaitHandle, which
// releases exactly one thread before resetting,
// because it was created with AutoReset mode.
// SignalAndWait then blocks on clearCount, to
// allow the signaled thread to decrement the count
// before looping again.
//
WaitHandle.SignalAndWait(ewh, clearCount);
}
Console.WriteLine();
// Create a ManualReset EventWaitHandle.
//
ewh = new EventWaitHandle(false, EventResetMode.ManualReset);
// Create and start five more numbered threads.
//
for(int i=0; i<=4; i++)
{
Thread t = new Thread(
new ParameterizedThreadStart(ThreadProc)
);
t.Start(i);
}
// Wait until all the threads have started and blocked.
//
while (Interlocked.Read(ref threadCount) < 5)
{
Thread.Sleep(500);
}
// Because the EventWaitHandle was created with
// ManualReset mode, signaling it releases all the
// waiting threads.
//
Console.WriteLine("Press ENTER to release the waiting threads.");
Console.ReadLine();
ewh.Set();
}
public static void ThreadProc(object data)
{
int index = (int) data;
Console.WriteLine("Thread {0} blocks.", data);
// Increment the count of blocked threads.
Interlocked.Increment(ref threadCount);
// Wait on the EventWaitHandle.
ewh.WaitOne();
Console.WriteLine("Thread {0} exits.", data);
// Decrement the count of blocked threads.
Interlocked.Decrement(ref threadCount);
// After signaling ewh, the main thread blocks on
// clearCount until the signaled thread has
// decremented the count. Signal it now.
//
clearCount.Set();
}
}
Imports System.Threading
Public Class Example
' The EventWaitHandle used to demonstrate the difference
' between AutoReset and ManualReset synchronization events.
'
Private Shared ewh As EventWaitHandle
' A counter to make sure all threads are started and
' blocked before any are released. A Long is used to show
' the use of the 64-bit Interlocked methods.
'
Private Shared threadCount As Long = 0
' An AutoReset event that allows the main thread to block
' until an exiting thread has decremented the count.
'
Private Shared clearCount As New EventWaitHandle(False, _
EventResetMode.AutoReset)
<MTAThread> _
Public Shared Sub Main()
' Create an AutoReset EventWaitHandle.
'
ewh = New EventWaitHandle(False, EventResetMode.AutoReset)
' Create and start five numbered threads. Use the
' ParameterizedThreadStart delegate, so the thread
' number can be passed as an argument to the Start
' method.
For i As Integer = 0 To 4
Dim t As New Thread(AddressOf ThreadProc)
t.Start(i)
Next i
' Wait until all the threads have started and blocked.
' When multiple threads use a 64-bit value on a 32-bit
' system, you must access the value through the
' Interlocked class to guarantee thread safety.
'
While Interlocked.Read(threadCount) < 5
Thread.Sleep(500)
End While
' Release one thread each time the user presses ENTER,
' until all threads have been released.
'
While Interlocked.Read(threadCount) > 0
Console.WriteLine("Press ENTER to release a waiting thread.")
Console.ReadLine()
' SignalAndWait signals the EventWaitHandle, which
' releases exactly one thread before resetting,
' because it was created with AutoReset mode.
' SignalAndWait then blocks on clearCount, to
' allow the signaled thread to decrement the count
' before looping again.
'
WaitHandle.SignalAndWait(ewh, clearCount)
End While
Console.WriteLine()
' Create a ManualReset EventWaitHandle.
'
ewh = New EventWaitHandle(False, EventResetMode.ManualReset)
' Create and start five more numbered threads.
'
For i As Integer = 0 To 4
Dim t As New Thread(AddressOf ThreadProc)
t.Start(i)
Next i
' Wait until all the threads have started and blocked.
'
While Interlocked.Read(threadCount) < 5
Thread.Sleep(500)
End While
' Because the EventWaitHandle was created with
' ManualReset mode, signaling it releases all the
' waiting threads.
'
Console.WriteLine("Press ENTER to release the waiting threads.")
Console.ReadLine()
ewh.Set()
End Sub
Public Shared Sub ThreadProc(ByVal data As Object)
Dim index As Integer = CInt(data)
Console.WriteLine("Thread {0} blocks.", data)
' Increment the count of blocked threads.
Interlocked.Increment(threadCount)
' Wait on the EventWaitHandle.
ewh.WaitOne()
Console.WriteLine("Thread {0} exits.", data)
' Decrement the count of blocked threads.
Interlocked.Decrement(threadCount)
' After signaling ewh, the main thread blocks on
' clearCount until the signaled thread has
' decremented the count. Signal it now.
'
clearCount.Set()
End Sub
End Class
備註
如果事件的初始狀態是無訊號的,等待事件的執行緒將會阻塞。 若初始狀態被信號,且 ManualReset 標記為 mode,等待事件的執行緒不會阻塞。 若初始狀態被 信號modeAutoReset為 ,第一個等待該事件的執行緒會立即被釋放,之後事件會重置,後續執行緒會阻塞。
另請參閱
適用於
EventWaitHandle(Boolean, EventResetMode, String)
初始化該類別的新實例 EventWaitHandle ,指定若因呼叫而建立等待句柄,是否會先發出訊號,是自動或手動重置,以及系統同步事件名稱。
public:
EventWaitHandle(bool initialState, System::Threading::EventResetMode mode, System::String ^ name);
[System.Security.SecurityCritical]
public EventWaitHandle(bool initialState, System.Threading.EventResetMode mode, string name);
public EventWaitHandle(bool initialState, System.Threading.EventResetMode mode, string? name);
public EventWaitHandle(bool initialState, System.Threading.EventResetMode mode, string name);
[<System.Security.SecurityCritical>]
new System.Threading.EventWaitHandle : bool * System.Threading.EventResetMode * string -> System.Threading.EventWaitHandle
new System.Threading.EventWaitHandle : bool * System.Threading.EventResetMode * string -> System.Threading.EventWaitHandle
Public Sub New (initialState As Boolean, mode As EventResetMode, name As String)
參數
- initialState
- Boolean
true 若該事件 false 因呼叫產生,初始狀態為已訊號;將初始狀態設為非訊號。
- mode
- EventResetMode
EventResetMode這是決定事件是自動重置還是手動重置的數值之一。
- name
- String
若同步物件要與其他程序共享,則使用名稱;否則, null 或空字串。 名稱是區分大小寫的。 反斜線字元(\)是保留的,只能用來指定命名空間。 欲了解更多命名空間資訊,請參閱備註區。 根據作業系統不同,名稱可能還有進一步限制。 例如,在基於 Unix 的作業系統中,排除命名空間後的名稱必須是有效的檔名。
- 屬性
例外狀況
name 無效。 這可能有各種原因,包括作業系統可能施加的一些限制,例如未知的前綴或無效字元。 請注意,名稱及常見前綴「Global\」與「Local\」皆為大小寫區分。
-或-
還有其他錯誤。 房產 HResult 可能會提供更多資訊。
Windows 僅有:name 指定了一個未知命名空間。 更多資訊請參閱 物件名稱 。
name太長了。 長度限制可能依作業系統或設定而異。
命名事件存在且具備存取控制安全性,但使用者沒有 FullControl。
無法建立與所提供的 name 同步物件。 不同類型的同步物件可能名稱相同。
備註
可以name以 或 Global\ 作為前綴Local\,以指定命名空間。 當 Global 命名空間被指定時,同步物件可以與系統上的任何程序共享。 當 Local 指定命名空間時(這也是未指定命名空間時的預設),同步物件可以與同一會話中的程序共享。 在 Windows 上,會話是登入會話,服務通常在不同的非互動會話中執行。 在類 Unix 作業系統中,每個 shell 都有自己的會話。 會話本地同步物件可能適合用於在父/子關係中同步,且所有程序皆在同一會話中執行。 欲了解更多關於Windows同步物件名稱的資訊,請參見 Object Names。
如果提供 a name ,且該命名空間中已有該請求類型的同步物件,則會開啟該現有的同步物件。 如果命名空間中已有不同類型的同步物件,則會拋出 a WaitHandleCannotBeOpenedException 。 否則,會建立一個新的同步物件。
若系統事件已存在且參數名稱已指定 name ,則忽略該 initialState 參數。
注意事項
預設情況下,命名事件不僅限於創建它的使用者。 其他使用者可能能夠開啟並使用該事件,包括透過不當設定或重置事件來干擾事件。 要限制特定使用者的存取,你可以使用建構器過載,或EventWaitHandleAcl在建立指定事件時傳遞 。EventWaitHandleSecurity 避免在可能有不受信任使用者執行程式碼的系統中使用無存取限制的命名事件。
Important
使用此構造子表示命名系統事件時,請指定 false 。initialState 此建構器無法判斷是否建立了命名系統事件,因此無法對命名事件的狀態做出任何假設。 要判斷是否建立了命名事件,請使用建構子或EventWaitHandle(Boolean, EventResetMode, String, Boolean)EventWaitHandle(Boolean, EventResetMode, String, Boolean, EventWaitHandleSecurity)建構子。
如果事件的初始狀態是無訊號的,等待事件的執行緒將會阻塞。 若初始狀態被信號,且 ManualReset 標記為 mode,等待事件的執行緒不會阻塞。 若初始狀態被 信號modeAutoReset為 ,第一個等待該事件的執行緒會立即被釋放,之後事件會重置,後續執行緒會阻塞。
另請參閱
適用於
EventWaitHandle(Boolean, EventResetMode, String, Boolean)
初始化該類別的新實例 EventWaitHandle ,指定若因呼叫建立等待句柄是否會被最初發出訊號,是否自動或手動重置,系統同步事件名稱,以及呼叫後的布林變數值,指示該系統事件是否被建立。
public:
EventWaitHandle(bool initialState, System::Threading::EventResetMode mode, System::String ^ name, [Runtime::InteropServices::Out] bool % createdNew);
[System.Security.SecurityCritical]
public EventWaitHandle(bool initialState, System.Threading.EventResetMode mode, string name, out bool createdNew);
public EventWaitHandle(bool initialState, System.Threading.EventResetMode mode, string? name, out bool createdNew);
public EventWaitHandle(bool initialState, System.Threading.EventResetMode mode, string name, out bool createdNew);
[<System.Security.SecurityCritical>]
new System.Threading.EventWaitHandle : bool * System.Threading.EventResetMode * string * bool -> System.Threading.EventWaitHandle
new System.Threading.EventWaitHandle : bool * System.Threading.EventResetMode * string * bool -> System.Threading.EventWaitHandle
Public Sub New (initialState As Boolean, mode As EventResetMode, name As String, ByRef createdNew As Boolean)
參數
- initialState
- Boolean
true 若該事件 false 因呼叫產生,初始狀態為已訊號;將初始狀態設為非訊號。
- mode
- EventResetMode
EventResetMode這是決定事件是自動重置還是手動重置的數值之一。
- name
- String
若同步物件要與其他程序共享,則使用名稱;否則, null 或空字串。 名稱是區分大小寫的。 反斜線字元(\)是保留的,只能用來指定命名空間。 欲了解更多命名空間資訊,請參閱備註區。 根據作業系統不同,名稱可能還有進一步限制。 例如,在基於 Unix 的作業系統中,排除命名空間後的名稱必須是有效的檔名。
- createdNew
- Boolean
當此方法回傳時,包含 true 若已建立本地事件(即 name 為 或 null 為空字串),或是否建立了指定的命名系統事件; false 若指定的命名系統事件已存在。 這個參數會未初始化傳遞。
- 屬性
例外狀況
name 無效。 這可能有各種原因,包括作業系統可能施加的一些限制,例如未知的前綴或無效字元。 請注意,名稱及常見前綴「Global\」與「Local\」皆為大小寫區分。
-或-
還有其他錯誤。 房產 HResult 可能會提供更多資訊。
Windows 僅有:name 指定了一個未知命名空間。 更多資訊請參閱 物件名稱 。
name太長了。 長度限制可能依作業系統或設定而異。
命名事件存在且具備存取控制安全性,但使用者沒有 FullControl。
無法建立與所提供的 name 同步物件。 不同類型的同步物件可能名稱相同。
備註
可以name以 或 Global\ 作為前綴Local\,以指定命名空間。 當 Global 命名空間被指定時,同步物件可以與系統上的任何程序共享。 當 Local 指定命名空間時(這也是未指定命名空間時的預設),同步物件可以與同一會話中的程序共享。 在 Windows 上,會話是登入會話,服務通常在不同的非互動會話中執行。 在類 Unix 作業系統中,每個 shell 都有自己的會話。 會話本地同步物件可能適合用於在父/子關係中同步,且所有程序皆在同一會話中執行。 欲了解更多關於Windows同步物件名稱的資訊,請參見 Object Names。
如果提供 a name ,且該命名空間中已有該請求類型的同步物件,則會開啟該現有的同步物件。 如果命名空間中已有不同類型的同步物件,則會拋出 a WaitHandleCannotBeOpenedException 。 否則,會建立一個新的同步物件。
若系統事件已存在且參數名稱已指定 name ,則忽略該 initialState 參數。 呼叫此建構子後,使用指定變數中的 ref參數(Visual Basic 中的 ByRef參數)createdNew 來判斷該命名系統事件是否已存在或已被創建。
如果事件的初始狀態是無訊號的,等待事件的執行緒將會阻塞。 若初始狀態被信號,且 ManualReset 標記為 mode,等待事件的執行緒不會阻塞。 若初始狀態被 信號modeAutoReset為 ,第一個等待該事件的執行緒會立即被釋放,之後事件會重置,後續執行緒會阻塞。
注意事項
預設情況下,命名事件不僅限於創建它的使用者。 其他使用者可能能夠開啟並使用該事件,包括透過不當設定或重置事件來干擾事件。 要限制特定使用者的存取,你可以使用建構器過載,或EventWaitHandleAcl在建立指定事件時傳遞 。EventWaitHandleSecurity 避免在可能有不受信任使用者執行程式碼的系統中使用無存取限制的命名事件。
另請參閱
適用於
EventWaitHandle(Boolean, EventResetMode, String, NamedWaitHandleOptions)
初始化該類別的新實例 EventWaitHandle ,指定若因呼叫建立等待句柄,是否會先發出訊號,是否會自動或手動重置,系統同步事件名稱,以及設定使用者範圍與會話範圍存取的選項。
public:
EventWaitHandle(bool initialState, System::Threading::EventResetMode mode, System::String ^ name, System::Threading::NamedWaitHandleOptions options);
public EventWaitHandle(bool initialState, System.Threading.EventResetMode mode, string? name, System.Threading.NamedWaitHandleOptions options);
new System.Threading.EventWaitHandle : bool * System.Threading.EventResetMode * string * System.Threading.NamedWaitHandleOptions -> System.Threading.EventWaitHandle
Public Sub New (initialState As Boolean, mode As EventResetMode, name As String, options As NamedWaitHandleOptions)
參數
- initialState
- Boolean
true 若該事件 false 因呼叫產生,初始狀態為已訊號;將初始狀態設為非訊號。
- mode
- EventResetMode
EventResetMode這是決定事件是自動重置還是手動重置的數值之一。
- name
- String
若同步物件要與其他程序共享,則使用名稱;否則, null 或空字串。 名稱是區分大小寫的。
- options
- NamedWaitHandleOptions
命名帳號的範圍選項。 預設權限僅限於目前使用者和當前會話。 指定的選項可能會影響名稱的命名空間以及底層句柄物件的存取。
例外狀況
name 無效。 這可能有各種原因,包括作業系統可能施加的一些限制,例如未知的前綴或無效字元。 請注意,名稱及常見前綴「Global\」與「Local\」皆為大小寫區分。
-或-
還有其他錯誤。 房產 HResult 可能會提供更多資訊。
Windows 僅有:name 指定了一個未知命名空間。 更多資訊請參閱 物件名稱 。
name太長了。 長度限制可能依作業系統或設定而異。
命名事件存在且具備存取控制安全性,但使用者沒有 FullControl。
無法建立與所提供的 name 同步物件。 不同類型的同步物件可能名稱相同。
-或-
存在具有指定 name 條件的物件,但指定的 options 物件與現有物件的選項不相容。
mode列舉值超出法律範圍。
備註
如果提供 a name ,且該命名空間中已有該請求類型的同步物件,則會開啟該現有的同步物件。 然而,若 options 指定存取權限僅限於目前使用者,且同步物件與其不相容,則會拋出 a WaitHandleCannotBeOpenedException 。 如果命名空間中已經存在不同類型的同步物件,也會拋出 a WaitHandleCannotBeOpenedException 。 否則,會建立一個新的同步物件。
若系統事件已存在且參數名稱已指定 name ,則忽略該 initialState 參數。
Important
使用此構造子表示命名系統事件時,請指定 false 。initialState 這個建構器無法判斷是否真的產生了命名系統事件,因此你無法對命名事件的狀態做出任何假設。 要判斷是否建立了命名事件,請使用建構子或EventWaitHandle(Boolean, EventResetMode, String, Boolean)EventWaitHandle(Boolean, EventResetMode, String, Boolean, EventWaitHandleSecurity)建構子。
如果事件的初始狀態是無訊號的,等待事件的執行緒將會阻塞。 若初始狀態被標 ManualReset 示且標記為 mode,等待事件的執行緒不會阻塞。 若初始狀態被訊號且 mode 為 AutoReset,第一個等待事件的執行緒將立即被釋放,之後事件會重置,後續執行緒會阻塞。
在Windows時,可以提供 options 來指定該命名系統事件是僅對當前使用者開放,還是對所有使用者開放。 它也允許你指定該命名系統事件是僅對當前會話中的程序可存取,還是所有會話都能存取。 如需詳細資訊,請參閱NamedWaitHandleOptions。
注意事項
在基於 Unix 的作業系統中,該 options 參數不影響,因為命名系統事件不被支援。
另請參閱
適用於
EventWaitHandle(Boolean, EventResetMode, String, Boolean, EventWaitHandleSecurity)
初始化該類別的新實例 EventWaitHandle ,指定若因呼叫建立等待句柄,是否會先發出訊號,是否自動或手動重置,系統同步事件名稱,呼叫後的布林變數值表示該命名系統事件是否已建立,以及若該命名事件被建立,應套用的存取控制安全性。
public:
EventWaitHandle(bool initialState, System::Threading::EventResetMode mode, System::String ^ name, [Runtime::InteropServices::Out] bool % createdNew, System::Security::AccessControl::EventWaitHandleSecurity ^ eventSecurity);
public EventWaitHandle(bool initialState, System.Threading.EventResetMode mode, string name, out bool createdNew, System.Security.AccessControl.EventWaitHandleSecurity eventSecurity);
[System.Security.SecurityCritical]
public EventWaitHandle(bool initialState, System.Threading.EventResetMode mode, string name, out bool createdNew, System.Security.AccessControl.EventWaitHandleSecurity eventSecurity);
new System.Threading.EventWaitHandle : bool * System.Threading.EventResetMode * string * bool * System.Security.AccessControl.EventWaitHandleSecurity -> System.Threading.EventWaitHandle
[<System.Security.SecurityCritical>]
new System.Threading.EventWaitHandle : bool * System.Threading.EventResetMode * string * bool * System.Security.AccessControl.EventWaitHandleSecurity -> System.Threading.EventWaitHandle
Public Sub New (initialState As Boolean, mode As EventResetMode, name As String, ByRef createdNew As Boolean, eventSecurity As EventWaitHandleSecurity)
參數
- initialState
- Boolean
true 若該事件 false 因呼叫產生,初始狀態為已訊號;將初始狀態設為非訊號。
- mode
- EventResetMode
EventResetMode這是決定事件是自動重置還是手動重置的數值之一。
- name
- String
若同步物件要與其他程序共享,則使用名稱;否則, null 或空字串。 名稱是區分大小寫的。 反斜線字元(\)是保留的,只能用來指定命名空間。 欲了解更多命名空間資訊,請參閱備註區。 根據作業系統不同,名稱可能還有進一步限制。 例如,在基於 Unix 的作業系統中,排除命名空間後的名稱必須是有效的檔名。
- createdNew
- Boolean
當此方法回傳時,包含 true 若已建立本地事件(即 name 為 或 null 為空字串),或是否建立了指定的命名系統事件; false 若指定的命名系統事件已存在。 這個參數會未初始化傳遞。
- eventSecurity
- EventWaitHandleSecurity
一個 EventWaitHandleSecurity 代表要套用於指定系統事件的存取控制安全性的物件。
- 屬性
例外狀況
name 無效。 這可能有各種原因,包括作業系統可能施加的一些限制,例如未知的前綴或無效字元。 請注意,名稱及常見前綴「Global\」與「Local\」皆為大小寫區分。
-或-
還有其他錯誤。 房產 HResult 可能會提供更多資訊。
Windows 僅有:name 指定了一個未知命名空間。 更多資訊請參閱 物件名稱 。
name太長了。 長度限制可能依作業系統或設定而異。
命名事件存在且具備存取控制安全性,但使用者沒有 FullControl。
無法建立與所提供的 name 同步物件。 不同類型的同步物件可能名稱相同。
範例
以下程式碼範例展示了具名系統事件與存取控制安全性的跨程序行為。 範例中使用 OpenExisting(String) 方法過載來測試命名事件的存在。
若事件不存在,則以初始擁有權與存取控制安全建立,該安全機制剝奪當前使用者使用事件的權利,但賦予讀取與變更事件權限的權利。
如果你從兩個指令視窗執行編譯後的範例,第二個副本會在呼叫 的 OpenExisting(String)時拋出存取違規異常。 例外被捕捉,範例利用 OpenExisting(String, EventWaitHandleRights) 方法過載等待事件,並擁有讀取和變更權限所需的權限。
權限變更後,事件會被開啟,並擁有等待並發出訊號的權利。 如果你從第三個指令視窗執行編譯後的範例,範例會使用新的權限執行。
using System;
using System.Threading;
using System.Security.AccessControl;
internal class Example
{
internal static void Main()
{
const string ewhName = "EventWaitHandleExample5";
EventWaitHandle ewh = null;
bool doesNotExist = false;
bool unauthorized = false;
// The value of this variable is set by the event
// constructor. It is true if the named system event was
// created, and false if the named event already existed.
//
bool wasCreated;
// Attempt to open the named event.
try
{
// Open the event with (EventWaitHandleRights.Synchronize
// | EventWaitHandleRights.Modify), to wait on and
// signal the named event.
//
ewh = EventWaitHandle.OpenExisting(ewhName);
}
catch (WaitHandleCannotBeOpenedException)
{
Console.WriteLine("Named event does not exist.");
doesNotExist = true;
}
catch (UnauthorizedAccessException ex)
{
Console.WriteLine("Unauthorized access: {0}", ex.Message);
unauthorized = true;
}
// There are three cases: (1) The event does not exist.
// (2) The event exists, but the current user doesn't
// have access. (3) The event exists and the user has
// access.
//
if (doesNotExist)
{
// The event does not exist, so create it.
// Create an access control list (ACL) that denies the
// current user the right to wait on or signal the
// event, but allows the right to read and change
// security information for the event.
//
string user = Environment.UserDomainName + "\\"
+ Environment.UserName;
EventWaitHandleSecurity ewhSec =
new EventWaitHandleSecurity();
EventWaitHandleAccessRule rule =
new EventWaitHandleAccessRule(user,
EventWaitHandleRights.Synchronize |
EventWaitHandleRights.Modify,
AccessControlType.Deny);
ewhSec.AddAccessRule(rule);
rule = new EventWaitHandleAccessRule(user,
EventWaitHandleRights.ReadPermissions |
EventWaitHandleRights.ChangePermissions,
AccessControlType.Allow);
ewhSec.AddAccessRule(rule);
// Create an EventWaitHandle object that represents
// the system event named by the constant 'ewhName',
// initially signaled, with automatic reset, and with
// the specified security access. The Boolean value that
// indicates creation of the underlying system object
// is placed in wasCreated.
//
ewh = new EventWaitHandle(true,
EventResetMode.AutoReset,
ewhName,
out wasCreated,
ewhSec);
// If the named system event was created, it can be
// used by the current instance of this program, even
// though the current user is denied access. The current
// program owns the event. Otherwise, exit the program.
//
if (wasCreated)
{
Console.WriteLine("Created the named event.");
}
else
{
Console.WriteLine("Unable to create the event.");
return;
}
}
else if (unauthorized)
{
// Open the event to read and change the access control
// security. The access control security defined above
// allows the current user to do this.
//
try
{
ewh = EventWaitHandle.OpenExisting(ewhName,
EventWaitHandleRights.ReadPermissions |
EventWaitHandleRights.ChangePermissions);
// Get the current ACL. This requires
// EventWaitHandleRights.ReadPermissions.
EventWaitHandleSecurity ewhSec = ewh.GetAccessControl();
string user = Environment.UserDomainName + "\\"
+ Environment.UserName;
// First, the rule that denied the current user
// the right to enter and release the event must
// be removed.
EventWaitHandleAccessRule rule =
new EventWaitHandleAccessRule(user,
EventWaitHandleRights.Synchronize |
EventWaitHandleRights.Modify,
AccessControlType.Deny);
ewhSec.RemoveAccessRule(rule);
// Now grant the user the correct rights.
//
rule = new EventWaitHandleAccessRule(user,
EventWaitHandleRights.Synchronize |
EventWaitHandleRights.Modify,
AccessControlType.Allow);
ewhSec.AddAccessRule(rule);
// Update the ACL. This requires
// EventWaitHandleRights.ChangePermissions.
ewh.SetAccessControl(ewhSec);
Console.WriteLine("Updated event security.");
// Open the event with (EventWaitHandleRights.Synchronize
// | EventWaitHandleRights.Modify), the rights required
// to wait on and signal the event.
//
ewh = EventWaitHandle.OpenExisting(ewhName);
}
catch (UnauthorizedAccessException ex)
{
Console.WriteLine("Unable to change permissions: {0}",
ex.Message);
return;
}
}
// Wait on the event, and hold it until the program
// exits.
//
try
{
Console.WriteLine("Wait on the event.");
ewh.WaitOne();
Console.WriteLine("Event was signaled.");
Console.WriteLine("Press the Enter key to signal the event and exit.");
Console.ReadLine();
}
catch (UnauthorizedAccessException ex)
{
Console.WriteLine("Unauthorized access: {0}", ex.Message);
}
finally
{
ewh.Set();
}
}
}
Imports System.Threading
Imports System.Security.AccessControl
Friend Class Example
<MTAThread> _
Friend Shared Sub Main()
Const ewhName As String = "EventWaitHandleExample5"
Dim ewh As EventWaitHandle = Nothing
Dim doesNotExist as Boolean = False
Dim unauthorized As Boolean = False
' The value of this variable is set by the event
' constructor. It is True if the named system event was
' created, and False if the named event already existed.
'
Dim wasCreated As Boolean
' Attempt to open the named event.
Try
' Open the event with (EventWaitHandleRights.Synchronize
' Or EventWaitHandleRights.Modify), to wait on and
' signal the named event.
'
ewh = EventWaitHandle.OpenExisting(ewhName)
Catch ex As WaitHandleCannotBeOpenedException
Console.WriteLine("Named event does not exist.")
doesNotExist = True
Catch ex As UnauthorizedAccessException
Console.WriteLine("Unauthorized access: {0}", ex.Message)
unauthorized = True
End Try
' There are three cases: (1) The event does not exist.
' (2) The event exists, but the current user doesn't
' have access. (3) The event exists and the user has
' access.
'
If doesNotExist Then
' The event does not exist, so create it.
' Create an access control list (ACL) that denies the
' current user the right to wait on or signal the
' event, but allows the right to read and change
' security information for the event.
'
Dim user As String = Environment.UserDomainName _
& "\" & Environment.UserName
Dim ewhSec As New EventWaitHandleSecurity()
Dim rule As New EventWaitHandleAccessRule(user, _
EventWaitHandleRights.Synchronize Or _
EventWaitHandleRights.Modify, _
AccessControlType.Deny)
ewhSec.AddAccessRule(rule)
rule = New EventWaitHandleAccessRule(user, _
EventWaitHandleRights.ReadPermissions Or _
EventWaitHandleRights.ChangePermissions, _
AccessControlType.Allow)
ewhSec.AddAccessRule(rule)
' Create an EventWaitHandle object that represents
' the system event named by the constant 'ewhName',
' initially signaled, with automatic reset, and with
' the specified security access. The Boolean value that
' indicates creation of the underlying system object
' is placed in wasCreated.
'
ewh = New EventWaitHandle(True, _
EventResetMode.AutoReset, ewhName, _
wasCreated, ewhSec)
' If the named system event was created, it can be
' used by the current instance of this program, even
' though the current user is denied access. The current
' program owns the event. Otherwise, exit the program.
'
If wasCreated Then
Console.WriteLine("Created the named event.")
Else
Console.WriteLine("Unable to create the event.")
Return
End If
ElseIf unauthorized Then
' Open the event to read and change the access control
' security. The access control security defined above
' allows the current user to do this.
'
Try
ewh = EventWaitHandle.OpenExisting(ewhName, _
EventWaitHandleRights.ReadPermissions Or _
EventWaitHandleRights.ChangePermissions)
' Get the current ACL. This requires
' EventWaitHandleRights.ReadPermissions.
Dim ewhSec As EventWaitHandleSecurity = _
ewh.GetAccessControl()
Dim user As String = Environment.UserDomainName _
& "\" & Environment.UserName
' First, the rule that denied the current user
' the right to enter and release the event must
' be removed.
Dim rule As New EventWaitHandleAccessRule(user, _
EventWaitHandleRights.Synchronize Or _
EventWaitHandleRights.Modify, _
AccessControlType.Deny)
ewhSec.RemoveAccessRule(rule)
' Now grant the user the correct rights.
'
rule = New EventWaitHandleAccessRule(user, _
EventWaitHandleRights.Synchronize Or _
EventWaitHandleRights.Modify, _
AccessControlType.Allow)
ewhSec.AddAccessRule(rule)
' Update the ACL. This requires
' EventWaitHandleRights.ChangePermissions.
ewh.SetAccessControl(ewhSec)
Console.WriteLine("Updated event security.")
' Open the event with (EventWaitHandleRights.Synchronize
' Or EventWaitHandleRights.Modify), the rights required
' to wait on and signal the event.
'
ewh = EventWaitHandle.OpenExisting(ewhName)
Catch ex As UnauthorizedAccessException
Console.WriteLine("Unable to change permissions: {0}", _
ex.Message)
Return
End Try
End If
' Wait on the event, and hold it until the program
' exits.
'
Try
Console.WriteLine("Wait on the event.")
ewh.WaitOne()
Console.WriteLine("Event was signaled.")
Console.WriteLine("Press the Enter key to signal the event and exit.")
Console.ReadLine()
Catch ex As UnauthorizedAccessException
Console.WriteLine("Unauthorized access: {0}", _
ex.Message)
Finally
ewh.Set()
End Try
End Sub
End Class
備註
使用此建構子在命名系統事件建立時,對該事件套用存取控制安全性,防止其他程式碼控制該事件。
此建構子初始化一個 EventWaitHandle 代表系統事件的物件。 你可以建立多個 EventWaitHandle 代表相同系統事件的物件。
若系統事件不存在,則以指定的存取控制安全性建立事件。 若事件存在,指定的存取控制安全性將被忽略。
Note
即使EventWaitHandle呼叫者拒絕或未授予當前使用者部分存取權限,仍能完全控制新建立eventSecurity的物件。 然而,如果目前使用者嘗試使用建構子或 EventWaitHandle 方法,讓另一個 OpenExisting 物件代表同一事件,則會套用Windows存取控制安全性。
可以name以 或 Global\ 作為前綴Local\,以指定命名空間。 當 Global 命名空間被指定時,同步物件可以與系統上的任何程序共享。 當 Local 指定命名空間時(這也是未指定命名空間時的預設),同步物件可以與同一會話中的程序共享。 在 Windows 上,會話是登入會話,服務通常在不同的非互動會話中執行。 在類 Unix 作業系統中,每個 shell 都有自己的會話。 會話本地同步物件可能適合用於在父/子關係中同步,且所有程序皆在同一會話中執行。 欲了解更多關於Windows同步物件名稱的資訊,請參見 Object Names。
如果提供 a name ,且該命名空間中已有該請求類型的同步物件,則會開啟該現有的同步物件。 如果命名空間中已有不同類型的同步物件,則會拋出 a WaitHandleCannotBeOpenedException 。 否則,會建立一個新的同步物件。
若系統事件已存在且參數名稱已指定 name ,則忽略該 initialState 參數。 呼叫此建構子後,使用指定變數中的 ref(Visual Basic 中的 ByRef參數)createdNew 來判斷該系統事件是否已存在或已被產生。
如果事件的初始狀態是無訊號的,等待事件的執行緒將會阻塞。 若初始狀態被信號,且 ManualReset 標記為 mode,等待事件的執行緒不會阻塞。 若初始狀態被 信號modeAutoReset為 ,第一個等待該事件的執行緒會立即被釋放,之後事件會重置,後續執行緒會阻塞。
注意事項
預設情況下,命名事件不僅限於創建它的使用者。 其他使用者可能能夠開啟並使用該事件,包括透過不當設定或重置事件來干擾事件。 要限制特定使用者的存取權限,你可以在建立指定事件時傳遞 a EventWaitHandleSecurity 。 避免在可能有不受信任使用者執行程式碼的系統中使用無存取限制的命名事件。
另請參閱
適用於
EventWaitHandle(Boolean, EventResetMode, String, NamedWaitHandleOptions, Boolean)
初始化該類別的新實例 EventWaitHandle ,指定若因呼叫建立等待句柄,是否會先發出訊號,是否自動或手動重置,系統同步事件名稱,設定使用者範圍與會話範圍存取選項,以及呼叫後的布林變數,該變數值顯示該系統事件是否被建立。
public:
EventWaitHandle(bool initialState, System::Threading::EventResetMode mode, System::String ^ name, System::Threading::NamedWaitHandleOptions options, [Runtime::InteropServices::Out] bool % createdNew);
public EventWaitHandle(bool initialState, System.Threading.EventResetMode mode, string? name, System.Threading.NamedWaitHandleOptions options, out bool createdNew);
new System.Threading.EventWaitHandle : bool * System.Threading.EventResetMode * string * System.Threading.NamedWaitHandleOptions * bool -> System.Threading.EventWaitHandle
Public Sub New (initialState As Boolean, mode As EventResetMode, name As String, options As NamedWaitHandleOptions, ByRef createdNew As Boolean)
參數
- initialState
- Boolean
true 若該事件 false 因呼叫產生,初始狀態為已訊號;將初始狀態設為非訊號。
- mode
- EventResetMode
EventResetMode這是決定事件是自動重置還是手動重置的數值之一。
- name
- String
若同步物件要與其他程序共享,則使用名稱;否則, null 或空字串。 名稱是區分大小寫的。
- options
- NamedWaitHandleOptions
命名帳號的範圍選項。 預設權限僅限於目前使用者和當前會話。 指定的選項可能會影響名稱的命名空間以及底層句柄物件的存取。
- createdNew
- Boolean
當此方法回傳時,包含 true 若已建立本地事件(即 name 若是 或 null 空字串),或是否已建立指定的命名系統事件;包含 false 該指定系統事件已存在。 這個參數會未初始化傳遞。
例外狀況
name 無效。 這可能有各種原因,包括作業系統可能施加的一些限制,例如未知的前綴或無效字元。 請注意,名稱及常見前綴「Global\」與「Local\」皆為大小寫區分。
-或-
還有其他錯誤。 房產 HResult 可能會提供更多資訊。
Windows 僅有:name 指定了一個未知命名空間。 更多資訊請參閱 物件名稱 。
name太長了。 長度限制可能依作業系統或設定而異。
命名事件存在且具備存取控制安全性,但使用者沒有 FullControl。
無法建立與所提供的 name 同步物件。 不同類型的同步物件可能名稱相同。
-或-
存在具有指定 name 條件的物件,但指定的 options 物件與現有物件的選項不相容。
mode列舉值超出法律範圍。
備註
如果提供 a name ,且該命名空間中已有該請求類型的同步物件,則會開啟該現有的同步物件。 然而,若 options 指定存取權限僅限於目前使用者,且同步物件與其不相容,則會拋出 a WaitHandleCannotBeOpenedException 。 如果命名空間中已經存在不同類型的同步物件,也會拋出 a WaitHandleCannotBeOpenedException 。 否則,會建立一個新的同步物件。
若系統事件已存在且參數名稱已指定 name ,則忽略該 initialState 參數。 呼叫此建構子後,使用指定變數中的 ref(Visual Basic 中的 ByRef參數)createdNew 來判斷該系統事件是否已存在或已被產生。
如果事件的初始狀態是無訊號的,等待事件的執行緒將會阻塞。 若初始狀態被信號,且 ManualReset 標記為 mode,等待事件的執行緒不會阻塞。 若初始狀態被 信號modeAutoReset為 ,第一個等待該事件的執行緒會立即被釋放,之後事件會重置,後續執行緒會阻塞。
在Windows時,可以提供 options 來指定該系統事件是僅對當前使用者開放,還是所有使用者都能存取。 它也允許你指定該命名系統事件是僅對當前會話中的程序可存取,還是所有會話都能存取。 如需詳細資訊,請參閱NamedWaitHandleOptions。
注意事項
在基於 Unix 的作業系統中,該 options 參數不影響,因為命名系統事件不被支援。