mscorlib(4.0.0.0) API with additions
EventWaitHandle.cs
1 using Microsoft.Win32;
2 using Microsoft.Win32.SafeHandles;
3 using System.IO;
5 using System.Security;
8 
9 namespace System.Threading
10 {
12  [ComVisible(true)]
13  [__DynamicallyInvokable]
14  [HostProtection(SecurityAction.LinkDemand, Synchronization = true, ExternalThreading = true)]
15  public class EventWaitHandle : WaitHandle
16  {
21  [SecuritySafeCritical]
22  [__DynamicallyInvokable]
23  public EventWaitHandle(bool initialState, EventResetMode mode)
24  : this(initialState, mode, null)
25  {
26  }
27 
38  [SecurityCritical]
39  [__DynamicallyInvokable]
40  public EventWaitHandle(bool initialState, EventResetMode mode, string name)
41  {
42  if (name != null && 260 < name.Length)
43  {
44  throw new ArgumentException(Environment.GetResourceString("Argument_WaitHandleNameTooLong", name));
45  }
46  SafeWaitHandle safeWaitHandle = null;
47  switch (mode)
48  {
49  case EventResetMode.ManualReset:
50  safeWaitHandle = Win32Native.CreateEvent(null, isManualReset: true, initialState, name);
51  break;
52  case EventResetMode.AutoReset:
53  safeWaitHandle = Win32Native.CreateEvent(null, isManualReset: false, initialState, name);
54  break;
55  default:
56  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidFlag", name));
57  }
58  if (safeWaitHandle.IsInvalid)
59  {
60  int lastWin32Error = Marshal.GetLastWin32Error();
61  safeWaitHandle.SetHandleAsInvalid();
62  if (name != null && name.Length != 0 && 6 == lastWin32Error)
63  {
64  throw new WaitHandleCannotBeOpenedException(Environment.GetResourceString("Threading.WaitHandleCannotBeOpenedException_InvalidHandle", name));
65  }
66  __Error.WinIOError(lastWin32Error, name);
67  }
68  SetHandleInternal(safeWaitHandle);
69  }
70 
82  [SecurityCritical]
83  [__DynamicallyInvokable]
84  public EventWaitHandle(bool initialState, EventResetMode mode, string name, out bool createdNew)
85  : this(initialState, mode, name, out createdNew, null)
86  {
87  }
88 
101  [SecurityCritical]
102  public unsafe EventWaitHandle(bool initialState, EventResetMode mode, string name, out bool createdNew, EventWaitHandleSecurity eventSecurity)
103  {
104  if (name != null && 260 < name.Length)
105  {
106  throw new ArgumentException(Environment.GetResourceString("Argument_WaitHandleNameTooLong", name));
107  }
108  Win32Native.SECURITY_ATTRIBUTES sECURITY_ATTRIBUTES = null;
109  if (eventSecurity != null)
110  {
111  sECURITY_ATTRIBUTES = new Win32Native.SECURITY_ATTRIBUTES();
112  sECURITY_ATTRIBUTES.nLength = Marshal.SizeOf(sECURITY_ATTRIBUTES);
113  byte[] securityDescriptorBinaryForm = eventSecurity.GetSecurityDescriptorBinaryForm();
114  byte* ptr = stackalloc byte[(int)checked(unchecked((ulong)(uint)securityDescriptorBinaryForm.Length) * 1uL)];
115  Buffer.Memcpy(ptr, 0, securityDescriptorBinaryForm, 0, securityDescriptorBinaryForm.Length);
116  sECURITY_ATTRIBUTES.pSecurityDescriptor = ptr;
117  }
118  SafeWaitHandle safeWaitHandle = null;
119  bool isManualReset;
120  switch (mode)
121  {
122  case EventResetMode.ManualReset:
123  isManualReset = true;
124  break;
125  case EventResetMode.AutoReset:
126  isManualReset = false;
127  break;
128  default:
129  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidFlag", name));
130  }
131  safeWaitHandle = Win32Native.CreateEvent(sECURITY_ATTRIBUTES, isManualReset, initialState, name);
132  int lastWin32Error = Marshal.GetLastWin32Error();
133  if (safeWaitHandle.IsInvalid)
134  {
135  safeWaitHandle.SetHandleAsInvalid();
136  if (name != null && name.Length != 0 && 6 == lastWin32Error)
137  {
138  throw new WaitHandleCannotBeOpenedException(Environment.GetResourceString("Threading.WaitHandleCannotBeOpenedException_InvalidHandle", name));
139  }
140  __Error.WinIOError(lastWin32Error, name);
141  }
142  createdNew = (lastWin32Error != 183);
143  SetHandleInternal(safeWaitHandle);
144  }
145 
146  [SecurityCritical]
147  private EventWaitHandle(SafeWaitHandle handle)
148  {
149  SetHandleInternal(handle);
150  }
151 
163  [SecurityCritical]
164  [__DynamicallyInvokable]
165  public static EventWaitHandle OpenExisting(string name)
166  {
167  return OpenExisting(name, EventWaitHandleRights.Modify | EventWaitHandleRights.Synchronize);
168  }
169 
182  [SecurityCritical]
183  public static EventWaitHandle OpenExisting(string name, EventWaitHandleRights rights)
184  {
185  EventWaitHandle result;
186  switch (OpenExistingWorker(name, rights, out result))
187  {
188  case OpenExistingResult.NameNotFound:
190  case OpenExistingResult.NameInvalid:
191  throw new WaitHandleCannotBeOpenedException(Environment.GetResourceString("Threading.WaitHandleCannotBeOpenedException_InvalidHandle", name));
192  case OpenExistingResult.PathNotFound:
193  __Error.WinIOError(3, "");
194  return result;
195  default:
196  return result;
197  }
198  }
199 
212  [SecurityCritical]
213  [__DynamicallyInvokable]
214  public static bool TryOpenExisting(string name, out EventWaitHandle result)
215  {
216  return OpenExistingWorker(name, EventWaitHandleRights.Modify | EventWaitHandleRights.Synchronize, out result) == OpenExistingResult.Success;
217  }
218 
232  [SecurityCritical]
233  public static bool TryOpenExisting(string name, EventWaitHandleRights rights, out EventWaitHandle result)
234  {
235  return OpenExistingWorker(name, rights, out result) == OpenExistingResult.Success;
236  }
237 
238  [SecurityCritical]
239  private static OpenExistingResult OpenExistingWorker(string name, EventWaitHandleRights rights, out EventWaitHandle result)
240  {
241  if (name == null)
242  {
243  throw new ArgumentNullException("name", Environment.GetResourceString("ArgumentNull_WithParamName"));
244  }
245  if (name.Length == 0)
246  {
247  throw new ArgumentException(Environment.GetResourceString("Argument_EmptyName"), "name");
248  }
249  if (name != null && 260 < name.Length)
250  {
251  throw new ArgumentException(Environment.GetResourceString("Argument_WaitHandleNameTooLong", name));
252  }
253  result = null;
254  SafeWaitHandle safeWaitHandle = Win32Native.OpenEvent((int)rights, inheritHandle: false, name);
255  if (safeWaitHandle.IsInvalid)
256  {
257  int lastWin32Error = Marshal.GetLastWin32Error();
258  if (2 == lastWin32Error || 123 == lastWin32Error)
259  {
260  return OpenExistingResult.NameNotFound;
261  }
262  if (3 == lastWin32Error)
263  {
264  return OpenExistingResult.PathNotFound;
265  }
266  if (name != null && name.Length != 0 && 6 == lastWin32Error)
267  {
268  return OpenExistingResult.NameInvalid;
269  }
270  __Error.WinIOError(lastWin32Error, "");
271  }
272  result = new EventWaitHandle(safeWaitHandle);
273  return OpenExistingResult.Success;
274  }
275 
280  [SecuritySafeCritical]
281  [__DynamicallyInvokable]
282  public bool Reset()
283  {
284  bool flag = Win32Native.ResetEvent(safeWaitHandle);
285  if (!flag)
286  {
287  __Error.WinIOError();
288  }
289  return flag;
290  }
291 
296  [SecuritySafeCritical]
297  [__DynamicallyInvokable]
298  public bool Set()
299  {
300  bool flag = Win32Native.SetEvent(safeWaitHandle);
301  if (!flag)
302  {
303  __Error.WinIOError();
304  }
305  return flag;
306  }
307 
313  [SecuritySafeCritical]
315  {
316  return new EventWaitHandleSecurity(safeWaitHandle, AccessControlSections.Access | AccessControlSections.Owner | AccessControlSections.Group);
317  }
318 
326  [SecuritySafeCritical]
327  public void SetAccessControl(EventWaitHandleSecurity eventSecurity)
328  {
329  if (eventSecurity == null)
330  {
331  throw new ArgumentNullException("eventSecurity");
332  }
333  eventSecurity.Persist(safeWaitHandle);
334  }
335  }
336 }
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
Encapsulates operating system–specific objects that wait for exclusive access to shared resources.
Definition: WaitHandle.cs:15
Represents the Windows access control security applied to a named system wait handle....
The exception that is thrown when an attempt is made to open a system mutex, semaphore,...
SafeWaitHandle SafeWaitHandle
Gets or sets the native operating system handle.
Definition: WaitHandle.cs:86
bool Set()
Sets the state of the event to signaled, allowing one or more waiting threads to proceed.
Definition: __Canon.cs:3
static int SizeOf(object structure)
Returns the unmanaged size of an object in bytes.
Definition: Marshal.cs:159
EventWaitHandle(bool initialState, EventResetMode mode, string name)
Initializes a new instance of the T:System.Threading.EventWaitHandle class, specifying whether the wa...
static bool TryOpenExisting(string name, EventWaitHandleRights rights, out EventWaitHandle result)
Opens the specified named synchronization event, if it already exists, with the desired security acce...
EventWaitHandle(bool initialState, EventResetMode mode)
Initializes a new instance of the T:System.Threading.EventWaitHandle class, specifying whether the wa...
void SetAccessControl(EventWaitHandleSecurity eventSecurity)
Sets the access control security for a named system event.
static bool TryOpenExisting(string name, out EventWaitHandle result)
Opens the specified named synchronization event, if it already exists, and returns a value that indic...
EventWaitHandle(bool initialState, EventResetMode mode, string name, out bool createdNew)
Initializes a new instance of the T:System.Threading.EventWaitHandle class, specifying whether the wa...
SecurityAction
Specifies the security actions that can be performed using declarative security.
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
unsafe EventWaitHandle(bool initialState, EventResetMode mode, string name, out bool createdNew, EventWaitHandleSecurity eventSecurity)
Initializes a new instance of the T:System.Threading.EventWaitHandle class, specifying whether the wa...
Provides a collection of methods for allocating unmanaged memory, copying unmanaged memory blocks,...
Definition: Marshal.cs:15
EventWaitHandleSecurity GetAccessControl()
Gets an T:System.Security.AccessControl.EventWaitHandleSecurity object that represents the access con...
Represents a thread synchronization event.
The exception that is thrown when one of the arguments provided to a method is not valid.
static EventWaitHandle OpenExisting(string name, EventWaitHandleRights rights)
Opens the specified named synchronization event, if it already exists, with the desired security acce...
Manipulates arrays of primitive types.
Definition: Buffer.cs:11
EventResetMode
Indicates whether an T:System.Threading.EventWaitHandle is reset automatically or manually after rece...
static int GetLastWin32Error()
Returns the error code returned by the last unmanaged function that was called using platform invoke ...
EventWaitHandleRights
Specifies the access control rights that can be applied to named system event objects.
AccessControlSections
Specifies which sections of a security descriptor to save or load.
bool Reset()
Sets the state of the event to nonsignaled, causing threads to block.
static EventWaitHandle OpenExisting(string name)
Opens the specified named synchronization event, if it already exists.