EventWaitHandle.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Diagnostics;
  5. using System.Diagnostics.CodeAnalysis;
  6. using System.IO;
  7. namespace System.Threading
  8. {
  9. public partial class EventWaitHandle : WaitHandle
  10. {
  11. public EventWaitHandle(bool initialState, EventResetMode mode) :
  12. this(initialState, mode, null, out _)
  13. {
  14. }
  15. public EventWaitHandle(bool initialState, EventResetMode mode, string? name) :
  16. this(initialState, mode, name, out _)
  17. {
  18. }
  19. public EventWaitHandle(bool initialState, EventResetMode mode, string? name, out bool createdNew)
  20. {
  21. if (mode != EventResetMode.AutoReset && mode != EventResetMode.ManualReset)
  22. throw new ArgumentException(SR.Argument_InvalidFlag, nameof(mode));
  23. CreateEventCore(initialState, mode, name, out createdNew);
  24. }
  25. public static EventWaitHandle OpenExisting(string name)
  26. {
  27. EventWaitHandle? result;
  28. switch (OpenExistingWorker(name, out result))
  29. {
  30. case OpenExistingResult.NameNotFound:
  31. throw new WaitHandleCannotBeOpenedException();
  32. case OpenExistingResult.NameInvalid:
  33. throw new WaitHandleCannotBeOpenedException(SR.Format(SR.Threading_WaitHandleCannotBeOpenedException_InvalidHandle, name));
  34. case OpenExistingResult.PathNotFound:
  35. throw new DirectoryNotFoundException(SR.Format(SR.IO_PathNotFound_Path, name));
  36. default:
  37. Debug.Assert(result != null, "result should be non-null on success");
  38. return result;
  39. }
  40. }
  41. public static bool TryOpenExisting(string name, [NotNullWhen(true)] out EventWaitHandle? result)
  42. {
  43. return OpenExistingWorker(name, out result) == OpenExistingResult.Success;
  44. }
  45. }
  46. }