/// <summary> /// process all lists for expired timers /// calculate next sleep delay /// </summary> protected void ProcessExpiredTimers() { int delay = Int32.MaxValue; // i want to save CPU cycles. calculate current tick only once long currentTick = DateTime.Now.Ticks / TimerList.TICKS_IN_MILLISECOND; int idx = 0; TimerList timerList = default(TimerList); while (true) { lock (this) { if (idx < timerLists.Count) { timerList = timerLists[idx]; idx++; } else { break; } } int temp; timerList.ProcessExpiredTimers(currentTick, out temp); if (temp < delay) { delay = temp; } } sleepTimeout = delay; }
public void AddList(TimerList timerList) { lock (this) { timerLists.Add(timerList); } }
public void RemoveList(TimerList timerList) { lock (this) { timerLists.Remove(timerList); } }
protected void debugTimerTestThread() { // create set (timer task). initially empty TimerTask timerTask = new TimerTask("ShortTimers"); Console.WriteLine("Start timers " + DateTime.Now); // create two types of timers TimerList timers_5sec = new TimerList("5sec", 5 * 1000, 100, this.Timer5sHandler, timerTask); TimerList timers_30sec = new TimerList("30sec", 30 * 1000, 100, this.Timer30sHandler, timerTask); timerTask.Start(); // start some timers timers_5sec.Start(); timers_5sec.Start(); timers_5sec.Start(); Thread.Sleep(1 * 1000); timers_5sec.Start(); ITimer timer; long timerId; timers_30sec.Start(out timer, out timerId, null, false); timers_5sec.Start(); debugTimerShowCallback(null, null, null); // wait for the first timer to expire Thread.Sleep(10 * 1000); timers_30sec.Stop(timer, timerId); Thread.Sleep(30 * 1000); debugTimerShowCallback(null, null, null); // clean up timers_5sec.Dispose(); timers_30sec.Dispose(); timerTask.Dispose(); }
/// <summary> /// this methos should be called once by the application /// before any of the API will be used /// </summary> public static void Init() { TimerList.InitTimerId(); }