public class SuperTimer // : IDisposable { private System.Timers.Timer gTickTimer = new System.Timers.Timer(20); public UInt32 gTickCount; private UInt32 aStartTick; private UInt32 aDuration; private UInt32 aStopTick; private bool enable = false; public bool Enable { // Enable or Disable SuperTimer的計時功能 get { return enable; } set { enable = value; if (enable) gTickTimer.Enabled = true; else gTickTimer.Enabled = false; } } // Constructor public SuperTimer() { gTickCount = 0; // Have the timer fire repeated events (true is the default) gTickTimer.AutoReset = true; // Hook up the Elapsed event for the timer. gTickTimer.Elapsed += gTickTimer_Elapsed; // Start the timer gTickTimer.Enabled = false; Console.WriteLine("Super timer construction"); } private void gTickTimer_Elapsed(Object source, System.Timers.ElapsedEventArgs e) { gTickCount += 1; } public void StartTimer(UInt32 inDurationMS) { gTickCount = 0; // 重新開始計數 aStartTick = gTickCount; aDuration = inDurationMS / 20; // 每20mS回Call一次 aStopTick = aStartTick + aDuration; } public bool TimerExpired() { if ((aStopTick == 0) || (gTickCount >= aStopTick)) { aStopTick = 0; return (true); } else return (false); } bool disposed; protected virtual void Dispose(bool disposing) { if (!disposed) { if (disposing) { //dispose managed resources } } //dispose unmanaged resources disposed = true; } public void Dispose() { gTickTimer.Stop(); gTickTimer.Dispose(); Dispose(true); GC.SuppressFinalize(this); Console.WriteLine("Super timer dispose was called"); } ~SuperTimer() { Console.WriteLine("Super timer destructor was called"); Dispose(false); } }