예제 #1
0
        public EventWaitHandle(bool initialState, EventResetMode mode, string name)
        {
            if (null != name)
            {
                if (((int)Interop.Constants.MaxPath) < name.Length)
                {
                    throw new ArgumentException(SR.Format(SR.Argument_WaitHandleNameTooLong, name));
                }
            }
            Contract.EndContractBlock();

            uint eventFlags = initialState ? (uint)Interop.Constants.CreateEventInitialSet : 0;

            IntPtr unsafeHandle;
            switch (mode)
            {
                case EventResetMode.ManualReset:
                    eventFlags |= (uint)Interop.Constants.CreateEventManualReset;
                    break;

                case EventResetMode.AutoReset:
                    break;

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

            unsafeHandle = Interop.mincore.CreateEventEx(IntPtr.Zero, name, eventFlags, (uint)Interop.Constants.EventAllAccess);
            int errorCode = (int)Interop.mincore.GetLastError();
            SafeWaitHandle _handle = new SafeWaitHandle(unsafeHandle, true);

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

                throw ExceptionFromCreationError(errorCode, name);
            }
            SafeWaitHandle = _handle;
        }
예제 #2
0
        public EventWaitHandle(bool initialState, EventResetMode mode, string name)
        {
            if(null != name && System.IO.Path.MAX_PATH < name.Length)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_WaitHandleNameTooLong",name));
            }
            Contract.EndContractBlock();
            
            SafeWaitHandle _handle = null;
#if MONO
            int errorCode;
#endif
            switch(mode)
            {
                case EventResetMode.ManualReset:
#if MONO
                    _handle = new SafeWaitHandle (NativeEventCalls.CreateEvent_internal (true, initialState, name, out errorCode), true);
#else
                    _handle = Win32Native.CreateEvent(null, true, initialState, name);
#endif
                    break;
                case EventResetMode.AutoReset:
#if MONO
                    _handle = new SafeWaitHandle (NativeEventCalls.CreateEvent_internal (false, initialState, name, out errorCode), true);
#else
                    _handle = Win32Native.CreateEvent(null, false, initialState, name);
#endif
                    break;

                default:
                    throw new ArgumentException(Environment.GetResourceString("Argument_InvalidFlag",name));
            };
                
            if (_handle.IsInvalid)
            {
#if !MONO
                int errorCode = Marshal.GetLastWin32Error();
#endif
            
                _handle.SetHandleAsInvalid();
                if(null != name && 0 != name.Length && Win32Native.ERROR_INVALID_HANDLE == errorCode)
                    throw new WaitHandleCannotBeOpenedException(Environment.GetResourceString("Threading.WaitHandleCannotBeOpenedException_InvalidHandle",name));

                __Error.WinIOError(errorCode, name);
            }
            SetHandleInternal(_handle);
        }
예제 #3
0
        public unsafe EventWaitHandle(bool initialState, EventResetMode mode, string name, out bool createdNew, EventWaitHandleSecurity eventSecurity)
        {
            if(null != name && System.IO.Path.MAX_PATH < name.Length)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_WaitHandleNameTooLong",name));
            }
            Contract.EndContractBlock();
#if !MONO
            Win32Native.SECURITY_ATTRIBUTES secAttrs = null;

#if FEATURE_MACL
            // For ACL's, get the security descriptor from the EventWaitHandleSecurity.
            if (eventSecurity != null) {
                secAttrs = new Win32Native.SECURITY_ATTRIBUTES();
                secAttrs.nLength = (int)Marshal.SizeOf(secAttrs);

                byte[] sd = eventSecurity.GetSecurityDescriptorBinaryForm();
                byte* pSecDescriptor = stackalloc byte[sd.Length];
                Buffer.Memcpy(pSecDescriptor, 0, sd, 0, sd.Length);
                secAttrs.pSecurityDescriptor = pSecDescriptor;
            }
#endif
#endif

            SafeWaitHandle _handle = null;
            Boolean isManualReset;
            switch(mode)
            {
                case EventResetMode.ManualReset:
                    isManualReset = true;
                    break;
                case EventResetMode.AutoReset:
                    isManualReset = false;
                    break;

                default:
                    throw new ArgumentException(Environment.GetResourceString("Argument_InvalidFlag",name));
            };

#if MONO
            int errorCode;
            _handle = new SafeWaitHandle (NativeEventCalls.CreateEvent_internal (isManualReset, initialState, name, out errorCode), true);
#else
            _handle = Win32Native.CreateEvent(secAttrs, isManualReset, initialState, name);
            int errorCode = Marshal.GetLastWin32Error();
#endif

            if (_handle.IsInvalid)
            {

                _handle.SetHandleAsInvalid();
                if(null != name && 0 != name.Length && Win32Native.ERROR_INVALID_HANDLE == errorCode)
                    throw new WaitHandleCannotBeOpenedException(Environment.GetResourceString("Threading.WaitHandleCannotBeOpenedException_InvalidHandle",name));

                __Error.WinIOError(errorCode, name);
            }
            createdNew = errorCode != Win32Native.ERROR_ALREADY_EXISTS;
            SetHandleInternal(_handle);
        }