public void AddEvent(ReactorEvent newEvent)
 {
     Monitor.Enter(_events);
     _events.Enqueue(newEvent);
     Monitor.Pulse(_events);
     Monitor.Exit(_events);
 }
        /// <summary>
        ///     Return the next event or null if there are no events and the
        ///     timeout milliseconds have passed.
        /// </summary>
        /// <param name="timeout"></param>
        /// <returns></returns>
        private ReactorEvent GetNextEvent(int timeout)
        {
            if (timeout > 20)
            {
                timeout = 20;
            }

            ReactorEvent nextEvent = null;

            // If there's an event queued now, return the next immediately.
            Monitor.Enter(_events);
            if (_events.Count > 0)
            {
                nextEvent = _events.Dequeue();
            }
            Monitor.Exit(_events);

            if (nextEvent != null)
            {
                return(nextEvent);
            }

            // If no timeout because lot's of pending timers, return immediately.
            if (timeout == 0)
            {
                return(null);
            }

            // Wait for another event to be queued or for the timeout
            // to expire.
            Monitor.Enter(_events);
            if (timeout > 0)
            {
                Monitor.Wait(_events, timeout);
            }
            else
            {
                Monitor.Wait(_events);
            }
            if (_events.Count > 0)
            {
                nextEvent = _events.Dequeue();
            }
            Monitor.Exit(_events);

            return(nextEvent);
        }