Esempio n. 1
0
        internal void Wait(object state)
        {
            try
            {
                WaitHandle[] waits = new WaitHandle[] { _waitObject, _cancelEvent };
                do
                {
                    int signal = WaitHandle.WaitAny(waits, _timeout, false);
                    if (!_unregistered)
                    {
                        lock (this) { _callsInProcess++; }
                        ThreadPool.QueueUserWorkItem(new WaitCallback(DoCallBack), (signal == WaitHandle.WaitTimeout));
                    }
                }while (!_unregistered && !_executeOnlyOnce);
            }
            catch {}

            lock (this) {
                _unregistered = true;
                if (_callsInProcess == 0 && _finalEvent != null)
                {
                    NativeEventCalls.SetEvent_internal(_finalEvent.Handle);
                }
            }
        }
Esempio n. 2
0
        private static OpenExistingResult OpenExistingWorker(string name, EventWaitHandleRights rights, out EventWaitHandle result)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name", Environment.GetResourceString("ArgumentNull_WithParamName"));
            }

            if (name.Length == 0)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_EmptyName"), "name");
            }

            if (null != name && System.IO.Path.MAX_PATH < name.Length)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_WaitHandleNameTooLong", name));
            }

            Contract.EndContractBlock();

            result = null;

#if MOBILE
            throw new NotSupportedException();
#else
#if MONO
            int errorCode;
            var myHandle = new SafeWaitHandle(NativeEventCalls.OpenEvent_internal(name, rights, out errorCode), true);
#else
#if FEATURE_MACL
            SafeWaitHandle myHandle = Win32Native.OpenEvent((int)rights, false, name);
#else
            SafeWaitHandle myHandle = Win32Native.OpenEvent(Win32Native.EVENT_MODIFY_STATE | Win32Native.SYNCHRONIZE, false, name);
#endif
#endif

            if (myHandle.IsInvalid)
            {
#if !MONO
                int errorCode = Marshal.GetLastWin32Error();
#endif

                if (Win32Native.ERROR_FILE_NOT_FOUND == errorCode || Win32Native.ERROR_INVALID_NAME == errorCode)
                {
                    return(OpenExistingResult.NameNotFound);
                }
                if (Win32Native.ERROR_PATH_NOT_FOUND == errorCode)
                {
                    return(OpenExistingResult.PathNotFound);
                }
                if (null != name && 0 != name.Length && Win32Native.ERROR_INVALID_HANDLE == errorCode)
                {
                    return(OpenExistingResult.NameInvalid);
                }
                //this is for passed through Win32Native Errors
                __Error.WinIOError(errorCode, "");
            }
            result = new EventWaitHandle(myHandle);
            return(OpenExistingResult.Success);
#endif
        }
Esempio n. 3
0
        public EventWaitHandle(bool initialState, EventResetMode mode)
        {
            bool created;
            bool manual = IsManualReset(mode);

            Handle = NativeEventCalls.CreateEvent_internal(manual, initialState, null, out created);
        }
Esempio n. 4
0
        public EventWaitHandle(bool initialState, EventResetMode mode,
                               string name, out bool createdNew)
        {
            bool manual = IsManualReset(mode);

            Handle = NativeEventCalls.CreateEvent_internal(manual, initialState, name, out createdNew);
        }
        /// <summary>Initializes a new instance of the <see cref="T:System.Threading.EventWaitHandle" /> class, specifying whether the wait handle is initially signaled, and whether it resets automatically or manually.</summary>
        /// <param name="initialState">true to set the initial state to signaled; false to set it to nonsignaled.</param>
        /// <param name="mode">One of the <see cref="T:System.Threading.EventResetMode" /> values that determines whether the event resets automatically or manually.</param>
        public EventWaitHandle(bool initialState, EventResetMode mode)
        {
            bool manual = this.IsManualReset(mode);
            bool flag;

            this.Handle = NativeEventCalls.CreateEvent_internal(manual, initialState, null, out flag);
        }
Esempio n. 6
0
        public static EventWaitHandle OpenExisting(string name, EventWaitHandleRights rights)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            if ((name.Length == 0) ||
                (name.Length > 260))
            {
                throw new ArgumentException("name", Locale.GetText("Invalid length [1-260]."));
            }

            MonoIOError error;
            IntPtr      handle = NativeEventCalls.OpenEvent_internal(name, rights, out error);

            if (handle == (IntPtr)null)
            {
                if (error == MonoIOError.ERROR_FILE_NOT_FOUND)
                {
                    throw new WaitHandleCannotBeOpenedException(Locale.GetText("Named Event handle does not exist: ") + name);
                }
                else if (error == MonoIOError.ERROR_ACCESS_DENIED)
                {
                    throw new UnauthorizedAccessException();
                }
                else
                {
                    throw new IOException(Locale.GetText("Win32 IO error: ") + error.ToString());
                }
            }

            return(new EventWaitHandle(handle));
        }
Esempio n. 7
0
 internal void Wait(object state)
 {
     try
     {
         WaitHandle[] waitHandles = new WaitHandle[]
         {
             this._waitObject,
             this._cancelEvent
         };
         do
         {
             int num = WaitHandle.WaitAny(waitHandles, this._timeout, false);
             if (!this._unregistered)
             {
                 lock (this)
                 {
                     this._callsInProcess++;
                 }
                 ThreadPool.QueueUserWorkItem(new WaitCallback(this.DoCallBack), num == 258);
             }
         }while (!this._unregistered && !this._executeOnlyOnce);
     }
     catch
     {
     }
     lock (this)
     {
         this._unregistered = true;
         if (this._callsInProcess == 0 && this._finalEvent != null)
         {
             NativeEventCalls.SetEvent_internal(this._finalEvent.Handle);
         }
     }
 }
Esempio n. 8
0
        public bool Set()
        {
            lock (this) {
                CheckDisposed();

                return(NativeEventCalls.SetEvent(SafeWaitHandle));
            }
        }
Esempio n. 9
0
        internal void SignalNoCallbacksRunning()
        {
#if !MONO
            Win32Native.SetEvent(m_notifyWhenNoCallbacksRunning.SafeWaitHandle);
#else
            NativeEventCalls.SetEvent_internal(m_notifyWhenNoCallbacksRunning.SafeWaitHandle.DangerousGetHandle());
#endif
        }
Esempio n. 10
0
        public bool Set()
        {
            lock (this) {
                CheckDisposed();

                return(NativeEventCalls.SetEvent_internal(Handle));
            }
        }
Esempio n. 11
0
        public bool Reset()
        {
            /* This needs locking since another thread could dispose the handle */
            lock (this) {
                CheckDisposed();

                return(NativeEventCalls.ResetEvent(SafeWaitHandle));
            }
        }
Esempio n. 12
0
 public bool Dispose(WaitHandle notifyObject)
 {
     if (notifyObject == null)
     {
         throw new ArgumentNullException("notifyObject");
     }
     Dispose();
     NativeEventCalls.SetEvent_internal(notifyObject.Handle);
     return(true);
 }
Esempio n. 13
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);
        }
Esempio n. 14
0
 private void DoCallBack(object timedOut)
 {
     if (this._callback != null)
     {
         this._callback(this._state, (bool)timedOut);
     }
     lock (this)
     {
         this._callsInProcess--;
         if (this._unregistered && this._callsInProcess == 0 && this._finalEvent != null)
         {
             NativeEventCalls.SetEvent_internal(this._finalEvent.Handle);
         }
     }
 }
Esempio n. 15
0
        [System.Security.SecuritySafeCritical]  // auto-generated
        public bool Reset()
        {
#if MONO
            var res = NativeEventCalls.ResetEvent(safeWaitHandle);
#else
            bool res = Win32Native.ResetEvent(safeWaitHandle);
#endif
            if (!res)
#if MONO
            { throw new IOException(); }
#else
            { __Error.WinIOError(); }
#endif
            return(res);
        }
Esempio n. 16
0
        internal void Wait(object state)
        {
            bool release = false;

            try {
                _waitObject.SafeWaitHandle.DangerousAddRef(ref release);
                try {
                    WaitHandle[] waits = new WaitHandle[] { _waitObject, _cancelEvent };
                    do
                    {
                        int signal = WaitHandle.WaitAny(waits, _timeout, false);
                        if (!_unregistered)
                        {
                            lock (this) {
                                _callsInProcess++;
                            }
                            ThreadPool.QueueUserWorkItem(new WaitCallback(DoCallBack), (signal == WaitHandle.WaitTimeout));
                        }
                    } while (!_unregistered && !_executeOnlyOnce);
                } catch {
                }

                lock (this) {
                    _unregistered = true;
                    if (_callsInProcess == 0 && _finalEvent != null)
                    {
#if NETCORE
                        throw new NotImplementedException();
#else
                        NativeEventCalls.SetEvent(_finalEvent.SafeWaitHandle);
                        _finalEvent = null;
#endif
                    }
                }
            } catch (ObjectDisposedException) {
                // Can happen if we called Unregister before we had time to execute Wait
                if (release)
                {
                    throw;
                }
            } finally {
                if (release)
                {
                    _waitObject.SafeWaitHandle.DangerousRelease();
                }
            }
        }
Esempio n. 17
0
        private void DoCallBack(object timedOut)
        {
            if (_callback != null)
            {
                try {
                    _callback(_state, (bool)timedOut);
                } catch {}
            }

            lock (this)
            {
                _callsInProcess--;
                if (_unregistered && _callsInProcess == 0 && _finalEvent != null)
                {
                    NativeEventCalls.SetEvent(_finalEvent.SafeWaitHandle);
                }
            }
        }
Esempio n. 18
0
        private void DoCallBack(object timedOut)
        {
            try {
                if (_callback != null)
                {
                    _callback(_state, (bool)timedOut);
                }
            } finally {
                lock (this)
                {
                    _callsInProcess--;
                    if (_unregistered && _callsInProcess == 0 && _finalEvent != null)
#if NETCORE
                    { EventWaitHandle.Set(_finalEvent.SafeWaitHandle); }
#else
                    { NativeEventCalls.SetEvent(_finalEvent.SafeWaitHandle); }
#endif
                }
            }
        }
Esempio n. 19
0
        protected virtual void Dispose(bool explicitDisposing)
        {
            // Check to see if Dispose has already been called.
            if (!disposed)
            {
                disposed = true;
                if (os_handle == InvalidHandle)
                {
                    return;
                }

                lock (this) {
                    if (os_handle != InvalidHandle)
                    {
                        NativeEventCalls.CloseEvent_internal(os_handle);
                        os_handle = InvalidHandle;
                    }
                }
            }
        }
Esempio n. 20
0
        public static bool TryOpenExisting(string name, EventWaitHandleRights rights,
                                           out EventWaitHandle result)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            if ((name.Length == 0) || (name.Length > 260))
            {
                throw new ArgumentException("name", Locale.GetText("Invalid length [1-260]."));
            }

            MonoIOError error;
            IntPtr      handle = NativeEventCalls.OpenEvent_internal(name, rights, out error);

            if (handle == (IntPtr)null)
            {
                result = null;
                return(false);
            }

            result = new EventWaitHandle(handle);
            return(true);
        }
Esempio n. 21
0
        public EventWaitHandle(bool initialState, EventResetMode mode)
        {
            bool created;

            Handle = NativeEventCalls.CreateEvent_internal((mode == EventResetMode.ManualReset), initialState, null, out created);
        }
Esempio n. 22
0
        public AutoResetEvent(bool initialState)
        {
            bool created;

            Handle = NativeEventCalls.CreateEvent_internal(false, initialState, null, out created);
        }
 /// <summary>Sets the state of the event to signaled, allowing one or more waiting threads to proceed.</summary>
 /// <returns>true if the operation succeeds; otherwise, false.</returns>
 /// <exception cref="T:System.ObjectDisposedException">The <see cref="M:System.Threading.EventWaitHandle.Close" /> method was previously called on this <see cref="T:System.Threading.EventWaitHandle" />.</exception>
 /// <filterpriority>2</filterpriority>
 public bool Set()
 {
     base.CheckDisposed();
     return(NativeEventCalls.SetEvent_internal(this.Handle));
 }
Esempio n. 24
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);
        }
Esempio n. 25
0
        public ManualResetEvent(bool initialState)
        {
            bool created;

            Handle = NativeEventCalls.CreateEvent_internal(true, initialState, null, out created);
        }
Esempio n. 26
0
 public bool Reset()
 {
     CheckDisposed();
     return(NativeEventCalls.ResetEvent_internal(Handle));
 }
Esempio n. 27
0
 public EventWaitHandle(bool initialState, EventResetMode mode,
                        string name, out bool createdNew,
                        EventWaitHandleSecurity eventSecurity)
 {
     Handle = NativeEventCalls.CreateEvent_internal((mode == EventResetMode.ManualReset), initialState, name, out createdNew);
 }
Esempio n. 28
0
 public bool Dispose(WaitHandle notifyObject)
 {
     Dispose();
     NativeEventCalls.SetEvent_internal(notifyObject.Handle);
     return(true);
 }