Seconds CountDown Timer

后端 未结 5 1049
花落未央
花落未央 2020-12-03 11:14

I have a lblCountdown with an int value of 60. I want to make the int value of the lblCountDown decrease with seconds until it reaches 0.

This is what I have so far:

5条回答
  •  被撕碎了的回忆
    2020-12-03 11:38

    .

    Usage:

    CountDownTimer timer = new CountDownTimer();
    
    
    //set to 30 mins
    timer.SetTime(30,0);     
    
    timer.Start();
    
    //update label text
    timer.TimeChanged += () => Label1.Text = timer.TimeLeftMsStr; 
    
    // show messageBox on timer = 00:00.000
    timer.CountDownFinished += () => MessageBox.Show("Timer finished the work!"); 
    
    //timer step. By default is 1 second
    timer.StepMs = 77; // for nice milliseconds time switch
    

    and don't forget to Dispose(); when timer is useless for you;

    Source code:

    using System;
    using System.Diagnostics;
    using System.Windows.Forms;
    
    public class CountDownTimer : IDisposable
    {
        public Stopwatch _stpWatch = new Stopwatch();
    
        public Action TimeChanged;
        public Action CountDownFinished;
    
        public bool IsRunnign => timer.Enabled;
    
        public int StepMs
        {
            get => timer.Interval;
            set => timer.Interval = value;
        }
    
        private Timer timer = new Timer();
    
        private TimeSpan _max = TimeSpan.FromMilliseconds(30000);
    
        public TimeSpan TimeLeft => (_max.TotalMilliseconds - _stpWatch.ElapsedMilliseconds) > 0 ? TimeSpan.FromMilliseconds(_max.TotalMilliseconds - _stpWatch.ElapsedMilliseconds) : TimeSpan.FromMilliseconds(0);
    
        private bool _mustStop => (_max.TotalMilliseconds - _stpWatch.ElapsedMilliseconds) < 0;
    
        public string TimeLeftStr => TimeLeft.ToString(@"\mm\:ss");
    
        public string TimeLeftMsStr => TimeLeft.ToString(@"mm\:ss\.fff");
    
        private void TimerTick(object sender, EventArgs e)
        {
            TimeChanged?.Invoke();
    
            if (_mustStop)
            {
                CountDownFinished?.Invoke();
                _stpWatch.Stop();
                timer.Enabled = false;
            }
        }
    
        public CountDownTimer(int min, int sec)
        {
            SetTime(min, sec);
            Init();
        }
    
        public CountDownTimer(TimeSpan ts)
        {
            SetTime(ts);
            Init();
        }
    
        public CountDownTimer()
        {
            Init();
        }
    
        private void Init()
        {
            StepMs = 1000;
            timer.Tick += new EventHandler(TimerTick);
        }
    
        public void SetTime(TimeSpan ts)
        {
            _max = ts;
            TimeChanged?.Invoke();
        }
    
        public void SetTime(int min, int sec = 0) => SetTime(TimeSpan.FromSeconds(min * 60 + sec));
    
        public void Start() {
            timer.Start();
            _stpWatch.Start();
        }
    
        public void Pause()
        {
            timer.Stop();
            _stpWatch.Stop();
        }
    
        public void Stop()
        {
            Reset();
            Pause();
        }
    
        public void Reset()
        {
            _stpWatch.Reset();
        }
    
        public void Restart()
        {
            _stpWatch.Reset();
            timer.Start();
        }
    
        public void Dispose() => timer.Dispose();
    }
    

    (updated 6.6.2020, because of problems with time calculation)

提交回复
热议问题