User Tools

Site Tools

blog:2023-05-09_c_supertimer_20ms_count_implement_improve



2023-05-09 C#: SuperTimer 20mS Count Implement Improve

  • Previous post I had implemented a SuperTimer class.
  • I add the Enable feature for the timer.

SuperTimer Code

  •     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);
            }
        }

TAGS

  • 57 person(s) visited this page until now.

Permalink blog/2023-05-09_c_supertimer_20ms_count_implement_improve.txt · Last modified: 2023/05/09 17:24 by jethro

oeffentlich