NEWS

[C#] Hướng dẫn sử dụng Count Down Timer Winform

[C#] Hướng dẫn sử dụng Count Down Timer Winform
Đăng bởi: Thảo Meo - Lượt xem: 12305 08:02:28, 23/10/2020ANDROID

Xin chào các bạn, bài viết hôm nay mình sẻ hướng dẫn các bạn sử dụng Count Download Timer dùng để đếm ngược thời gian trong lập trình C# Winform.

[C#] Using Count Down Timer

Countdown timer là gì ?

Countdown timer  đồng hồ đếm ngược. Bạn có thể sử dụng đồng hồ đếm ngược trên một trang website hay điện thoại di dộng của bạn.

Đồng hồ sẽ đếm ngược từ một số hoặc ngày nhất định để cho biết thời gian bắt đầu hoặc kết thúc một sự kiện.

Dưới đây là demo ứng dụng CountDown Timer khi mình click vào 20 phút thì sẽ đếm ngược thời gian.

count_down_timer_csharp

Đầu tiên mình tạo một class CountDownTimer.cs

using System;
using System.Diagnostics;
using System.Windows.Forms;

namespace CountDownTimerDemo
{
    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();
    }
}

Và cách sử dụng truyền số phút vào ứng dụng

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CountDownTimerDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            CountDownTimer timer = new CountDownTimer();         
            timer.SetTime(Convert.ToInt32(txt_minute.Text), 0);
            timer.Start();          
            timer.TimeChanged += () => label1.Text = timer.TimeLeftMsStr;       
            timer.CountDownFinished += () => label1.Text =  "Finish!";          
            timer.StepMs = 77; 
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

Thanks for watching!

DOWNLOAD SOURCE

Tags: countdowntimer c#timer c#

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Hướng dẫn sử dụng Count Down Timer Winform
Đăng bởi: Thảo Meo - Lượt xem: 12305 08:02:28, 23/10/2020ANDROID

CÁC BÀI CÙNG CHỦ ĐỀ

.