Esempio n. 1
0
 /// <summary>
 /// Cancels all timeouts associated with the given receiver destination.
 /// </summary>
 /// <param name="receiver">The destination of the timeout</param>
 /// <returns>The number of timeouts removed.</returns>
 public int CancelTimeoutByReceiver(ITimeoutReceiver receiver)
 {
     lock (_timeouts)
     {
         return(_timeouts.RemoveAll(t => t.Receiver == receiver));
     }
 }
Esempio n. 2
0
        /// <summary>
        /// Schedules a timeout event to occur in a given amount of time.
        /// </summary>
        /// <param name="receiver">The interface that will receive the timeout notification</param>
        /// <param name="state">An object that will be attached to the timeout event</param>
        /// <param name="due">The number of milliseconds that will elapse before the interface is notifies</param>
        /// <returns>A handle that identifies the timeout event</returns>
        public int ScheduleTimeout(ITimeoutReceiver receiver, object state, int due)
        {
            TimeoutInfo info = new TimeoutInfo()
            {
                State     = state,
                Receiver  = receiver,
                ExpiresAt = _utcNow().AddMilliseconds(due)
            };

            lock (_timeouts)
            {
                info.ID = _nextTimeoutId++;
                if (_nextTimeoutId >= int.MaxValue)
                {
                    _nextTimeoutId = 0;
                }

                // add the timeout in order
                var index = _timeouts.BinarySearch(info, info);
                if (index < 0)
                {
                    index = ~index;
                }
                _timeouts.Insert(index, info);
            }

            _recheckTimeout.Set();

            return(info.ID);
        }