Exemple #1
0
        internal unsafe EventWaitHandle(bool initialState, EventResetMode mode, string name, out bool createdNew, EventWaitHandleSecurity eventSecurity)
        {
            if (name != null)
            {
#if PLATFORM_UNIX
                throw new PlatformNotSupportedException(SR.PlatformNotSupported_NamedSynchronizationPrimitives);
#else
                if (System.IO.Path.MaxPath < name.Length)
                {
                    throw new ArgumentException(SR.Format(SR.Argument_WaitHandleNameTooLong, Path.MaxPath), nameof(name));
                }
#endif
            }
            Contract.EndContractBlock();
            Win32Native.SECURITY_ATTRIBUTES secAttrs = null;

            uint eventFlags = initialState ? Win32Native.CREATE_EVENT_INITIAL_SET : 0;
            switch (mode)
            {
            case EventResetMode.ManualReset:
                eventFlags |= Win32Native.CREATE_EVENT_MANUAL_RESET;
                break;

            case EventResetMode.AutoReset:
                break;

            default:
                throw new ArgumentException(SR.Format(SR.Argument_InvalidFlag, name));
            }
            ;

            SafeWaitHandle _handle = Win32Native.CreateEventEx(secAttrs, name, eventFlags, AccessRights);

            int errorCode = Marshal.GetLastWin32Error();
            if (_handle.IsInvalid)
            {
                _handle.SetHandleAsInvalid();
                if (null != name && 0 != name.Length && Win32Native.ERROR_INVALID_HANDLE == errorCode)
                {
                    throw new WaitHandleCannotBeOpenedException(SR.Format(SR.Threading_WaitHandleCannotBeOpenedException_InvalidHandle, name));
                }

                __Error.WinIOError(errorCode, name);
            }
            createdNew = errorCode != Win32Native.ERROR_ALREADY_EXISTS;
            SetHandleInternal(_handle);
        }
Exemple #2
0
        public EventWaitHandle(bool initialState, EventResetMode mode, string name)
        {
            if (name != null)
            {
#if PLATFORM_UNIX
                throw new PlatformNotSupportedException(SR.PlatformNotSupported_NamedSynchronizationPrimitives);
#else
                if (Interop.Kernel32.MAX_PATH < name.Length)
                {
                    throw new ArgumentException(SR.Format(SR.Argument_WaitHandleNameTooLong, name, Interop.Kernel32.MAX_PATH), nameof(name));
                }
#endif
            }

            uint eventFlags = initialState ? Win32Native.CREATE_EVENT_INITIAL_SET : 0;
            switch (mode)
            {
            case EventResetMode.ManualReset:
                eventFlags |= Win32Native.CREATE_EVENT_MANUAL_RESET;
                break;

            case EventResetMode.AutoReset:
                break;

            default:
                throw new ArgumentException(SR.Format(SR.Argument_InvalidFlag, name));
            }
            ;

            SafeWaitHandle _handle = Win32Native.CreateEventEx(null, name, eventFlags, AccessRights);

            if (_handle.IsInvalid)
            {
                int errorCode = Marshal.GetLastWin32Error();

                _handle.SetHandleAsInvalid();
                if (null != name && 0 != name.Length && Interop.Errors.ERROR_INVALID_HANDLE == errorCode)
                {
                    throw new WaitHandleCannotBeOpenedException(SR.Format(SR.Threading_WaitHandleCannotBeOpenedException_InvalidHandle, name));
                }

                throw Win32Marshal.GetExceptionForWin32Error(errorCode, name);
            }
            SetHandleInternal(_handle);
        }