EventWaitHandle.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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;
  5. using System.IO;
  6. namespace System.Threading
  7. {
  8. public partial class EventWaitHandle : WaitHandle
  9. {
  10. public EventWaitHandle(bool initialState, EventResetMode mode) :
  11. this(initialState, mode, null, out _)
  12. {
  13. }
  14. public EventWaitHandle(bool initialState, EventResetMode mode, string name) :
  15. this(initialState, mode, name, out _)
  16. {
  17. }
  18. public EventWaitHandle(bool initialState, EventResetMode mode, string name, out bool createdNew)
  19. {
  20. if (mode != EventResetMode.AutoReset && mode != EventResetMode.ManualReset)
  21. throw new ArgumentException(SR.Argument_InvalidFlag, nameof(mode));
  22. CreateEventCore(initialState, mode, name, out createdNew);
  23. }
  24. public static EventWaitHandle OpenExisting(string name)
  25. {
  26. EventWaitHandle result;
  27. switch (OpenExistingWorker(name, out result))
  28. {
  29. case OpenExistingResult.NameNotFound:
  30. throw new WaitHandleCannotBeOpenedException();
  31. case OpenExistingResult.NameInvalid:
  32. throw new WaitHandleCannotBeOpenedException(SR.Format(SR.Threading_WaitHandleCannotBeOpenedException_InvalidHandle, name));
  33. case OpenExistingResult.PathNotFound:
  34. throw new DirectoryNotFoundException(SR.Format(SR.IO_PathNotFound_Path, name));
  35. default:
  36. return result;
  37. }
  38. }
  39. public static bool TryOpenExisting(string name, out EventWaitHandle result)
  40. {
  41. return OpenExistingWorker(name, out result) == OpenExistingResult.Success;
  42. }
  43. }
  44. }