private void EventProcessor() { autoResetEvent.WaitOne(); while (true) { TimerQueueEvent minEvent = null; lock (synchNewEventLock) { minEvent = eventQueue.Peek(); } if (minEvent == null) { autoResetEvent.WaitOne(); } else { var waitTimeInMs = minEvent.TimeOut.TotalMilliseconds - NowInMilliSeconds; if (!autoResetEvent.WaitOne((int)waitTimeInMs)) { lock (synchNewEventLock) { minEvent = eventQueue.Pop(); } ThreadPool.QueueUserWorkItem(minEvent.CallBack, minEvent.State); } } } }
public void SetTimer(WaitCallback callBack, object state, double waitTimeInMs) { if (!initialized) { throw new InvalidOperationException( "method Initialize must be called before using the TimerQueue for scheduling actions"); } lock (synchNewEventLock) { newEvent = new TimerQueueEvent { CallBack = callBack, State = state, TimeOut = new TimeSpan(DateTime.Now.AddMilliseconds(waitTimeInMs).Ticks) }; eventQueue.Push(newEvent.TimeOut.TotalMilliseconds, newEvent); autoResetEvent.Set(); } ; }