EventWaitHandle.Windows.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. using System.Runtime.InteropServices;
  7. using Microsoft.Win32.SafeHandles;
  8. namespace System.Threading
  9. {
  10. public partial class EventWaitHandle
  11. {
  12. private const uint AccessRights = (uint)Interop.Kernel32.MAXIMUM_ALLOWED | Interop.Kernel32.SYNCHRONIZE | Interop.Kernel32.EVENT_MODIFY_STATE;
  13. private EventWaitHandle(SafeWaitHandle handle)
  14. {
  15. SafeWaitHandle = handle;
  16. }
  17. private void CreateEventCore(bool initialState, EventResetMode mode, string name, out bool createdNew)
  18. {
  19. #if !PLATFORM_WINDOWS
  20. if (name != null)
  21. throw new PlatformNotSupportedException(SR.PlatformNotSupported_NamedSynchronizationPrimitives);
  22. #endif
  23. uint eventFlags = initialState ? Interop.Kernel32.CREATE_EVENT_INITIAL_SET : 0;
  24. if (mode == EventResetMode.ManualReset)
  25. eventFlags |= (uint)Interop.Kernel32.CREATE_EVENT_MANUAL_RESET;
  26. SafeWaitHandle handle = Interop.Kernel32.CreateEventEx(IntPtr.Zero, name, eventFlags, AccessRights);
  27. int errorCode = Marshal.GetLastWin32Error();
  28. if (handle.IsInvalid)
  29. {
  30. handle.SetHandleAsInvalid();
  31. if (name != null && name.Length != 0 && errorCode == Interop.Errors.ERROR_INVALID_HANDLE)
  32. throw new WaitHandleCannotBeOpenedException(SR.Format(SR.Threading_WaitHandleCannotBeOpenedException_InvalidHandle, name));
  33. throw Win32Marshal.GetExceptionForWin32Error(errorCode, name);
  34. }
  35. createdNew = errorCode != Interop.Errors.ERROR_ALREADY_EXISTS;
  36. SafeWaitHandle = handle;
  37. }
  38. private static OpenExistingResult OpenExistingWorker(string name, out EventWaitHandle result)
  39. {
  40. #if PLATFORM_WINDOWS
  41. if (name == null)
  42. throw new ArgumentNullException(nameof(name));
  43. if (name.Length == 0)
  44. throw new ArgumentException(SR.Argument_EmptyName, nameof(name));
  45. result = null;
  46. SafeWaitHandle myHandle = Interop.Kernel32.OpenEvent(AccessRights, false, name);
  47. if (myHandle.IsInvalid)
  48. {
  49. int errorCode = Marshal.GetLastWin32Error();
  50. if (errorCode == Interop.Errors.ERROR_FILE_NOT_FOUND || errorCode == Interop.Errors.ERROR_INVALID_NAME)
  51. return OpenExistingResult.NameNotFound;
  52. if (errorCode == Interop.Errors.ERROR_PATH_NOT_FOUND)
  53. return OpenExistingResult.PathNotFound;
  54. if (name != null && name.Length != 0 && errorCode == Interop.Errors.ERROR_INVALID_HANDLE)
  55. return OpenExistingResult.NameInvalid;
  56. throw Win32Marshal.GetExceptionForWin32Error(errorCode, name);
  57. }
  58. result = new EventWaitHandle(myHandle);
  59. return OpenExistingResult.Success;
  60. #else
  61. throw new PlatformNotSupportedException(SR.PlatformNotSupported_NamedSynchronizationPrimitives);
  62. #endif
  63. }
  64. public bool Reset()
  65. {
  66. bool res = Interop.Kernel32.ResetEvent(_waitHandle);
  67. if (!res)
  68. throw Win32Marshal.GetExceptionForLastWin32Error();
  69. return res;
  70. }
  71. public bool Set()
  72. {
  73. bool res = Interop.Kernel32.SetEvent(_waitHandle);
  74. if (!res)
  75. throw Win32Marshal.GetExceptionForLastWin32Error();
  76. return res;
  77. }
  78. }
  79. }