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> /// Create a timer list /// </summary> /// <param name="name"> /// A <see cref="System.String"/> /// Name of the timer list /// </param> /// <param name="size"> /// A <see cref="System.Int32"/> /// Maximum number of pending timers /// </param> /// <param name="timeout"> /// A <see cref="System.Int32"/> /// Timeout for the timers in this list (milliseconds) /// </param> /// <param name="timerCallback"> /// A <see cref="TimerExpiredCallback"/> /// This method will be called for all expired timers /// There is no a callback per timer. Only a callback per timer list /// </param> public TimerList(string name, int timeout, int size, TimerExpiredCallback timerCallback, TimerTask timerTask) { Name = name; this.timerCallback = timerCallback; // this.baseTick = DateTime.Now.Ticks; this.timerTask = timerTask; this.Timeout = timeout; // clear the counters countStart = 0; countStartAttempt = 0; countStop = 0; countStopAttempt = 0; countExpired = 0; countMax = 0; // create pool of free timers InitTimers(size); // register with the TimerTask timerTask.AddList(this); // add myself to the list of created timer lists Resources.TimerLists.Add(this); }