/// <summary> /// Cancels an already started timeout. It will kill the timer on the thread pool as well as look through the queue /// to remove TimeoutMessageObject from the message queue if it's already been added. /// </summary> /// <param name="timeoutHandle">The handle identifying the timeout. It is the value return by the ScheduleTimeout method</param> public void CancelTimeout(int timeoutHandle) { if (_guard.EnterExecute()) { try { // remove from the list bool successfullyCancelOnPool = _pool.CancelTimeout(timeoutHandle); // the timeout just fired -- check to see if it's already on the queue. Remove it if it is. if (successfullyCancelOnPool == false) { lock (_messageQueue) { int messageCount = _messageQueue.Count; for (int i = 0; i < messageCount; i++) { var message = _messageQueue.Dequeue(); TimeoutMessageObject tm = message as TimeoutMessageObject; if (null == tm || tm.TimeoutId != timeoutHandle) { _messageQueue.Enqueue(message); } } } } } finally { _guard.ExitExecute(); } } }
/// <summary> /// The interface method of the thread pool. This is called when the timeout expires. It puts a TimeoutMessageObject onto the queue /// </summary> /// <param name="timerId">The identifier of the timeout.</param> /// <param name="state">The object that was passed in with the schedule.</param> void ITimeoutReceiver.ProcessTimeout(int timerId, object state) { var timer = state as TimeoutInfo; var message = new TimeoutMessageObject() { TimeoutId = timerId, startedAt = timer.startedAt, State = timer.timeoutState }; Push(message); }