2 using System.Collections.Generic;
9 /// A simple timer class. I'm not sure why .NET or at least
10 /// XNA doesn't already have one of these.
12 public class Timer<T> : IComparable<Timer<T>> where T : IComparable<T>
14 #region Public Properties
17 /// Get and set the expiration time. If the time is already set
18 /// and the timer is registered, you cannot push the expiration time
19 /// further back, but you can decrease it.
29 if (mTime.CompareTo(value) > 0)
32 gTimers.Promote(this);
38 /// Get the current absolute time, accurate up until
39 /// the last time Timer.FireAll was called.
41 public static T Now { get { return gNow; } }
44 /// Get and set the events that will be called when the
47 public event ExpiredEventHandler Expired;
50 /// Get and set a user contextual object.
52 public object Context;
60 /// The type for an event handler for timers that expire.
62 /// <param name="timer">The timer that expired.</param>
63 public delegate void ExpiredEventHandler(Timer<T> timer);
68 #region Public Methods
71 /// Construct a timer with an absolute time (in seconds)
72 /// when the timer should expire.
74 /// <param name="time">Absolute time, in seconds.</param>
82 /// Construct a timer with an absolute time (in seconds)
83 /// when the timer should expire, and an event handler.
85 /// <param name="time">Absolute time, in seconds.</param>
86 /// <param name="expired">An event handler.</param>
87 public Timer(T time, ExpiredEventHandler expired)
95 /// Construct a timer with an absolute time (in seconds)
96 /// when the timer should expire, and a contextual object.
98 /// <param name="time">Absolute time, in seconds.</param>
99 /// <param name="context">A user contextual object.</param>
100 public Timer(T time, object context)
108 /// Construct a timer with an absolute time (in seconds)
109 /// when the timer should expire, an event handler, and a
110 /// contextual object.
112 /// <param name="time">Absolute time, in seconds.</param>
113 /// <param name="expired">An event handler.</param>
114 /// <param name="context">A user contextual object.</param>
115 public Timer(T time, ExpiredEventHandler expired, object context)
125 /// Fire all outstanding timers which should be expired
126 /// before a certain time.
128 /// <param name="time">Absolute time, in seconds.</param>
129 public static void FireExpired(T time)
133 while (gTimers.Count > 0)
135 Timer<T> timer = gTimers.Peek();
136 if (timer.Time.CompareTo(Now) <= 0)
138 if (timer.Expired != null) timer.Expired(timer);
139 if (timer.Time.CompareTo(Now) <= 0) gTimers.GetNext();
150 /// Unregister all registered timers without firing any events.
152 public static void Clear()
159 /// Compare a timer with another, based on the expiration
160 /// times of both timers.
162 /// <param name="other">The timer to compare against.</param>
163 /// <returns>A negative value if this timer is less than the
164 /// other, a positive value if this timer is greater than the
165 /// other, or zero if they are the same.</returns>
166 public int CompareTo(Timer<T> other)
168 return Time.CompareTo(other.Time);
174 #region Private Variables
178 static IPriorityQueue<Timer<T>> gTimers = new BinaryHeap<Timer<T>>(10);