C# 计时器实现方法及计时器Timer控件,倒计时

借鉴帖子:https://blog.csdn.net/u013658041/article/details/78203931

 

system.Timers命名空间下的Timer类,使用Elapsed事件另开一个线程。定义一个System.Timers.Timer对象,然后绑定Elapsed事件,通过Start()方法来启动计时,通过Stop()方法或者Enable=false停止计时;

Mytimer.AutoReset = true; //每次达到指定间隔时间后,就触发System.Timers.Timer.Elapsed事件

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 Test1
{
    public partial class FormTimera : Form
    {
        public FormTimera()
        {
            InitializeComponent();
        }

        //定义Timer类变量
        System.Timers.Timer Mytimer;
        long TimeCount;
        //定义委托
        public delegate void SetControlValue(long value);




        private void FormTimera_Load(object sender, EventArgs e)
        {
            //设置时间间隔ms
            int interval = 1000;
            Mytimer = new System.Timers.Timer(interval);
            //设置重复计时
            Mytimer.AutoReset = true;
            //设置执行System.Timers.Timer.Elapsed事件

            Mytimer.Elapsed += new System.Timers.ElapsedEventHandler(Mytimer_tick);

        }

        //开始计时
        private void btnTimeStart_Click(object sender, EventArgs e)
        {
            Mytimer.Start();
            TimeCount = 0;

        }
        //停止计时
        private void btnTimeStop_Click(object sender, EventArgs e)
        {

            Mytimer.Stop();
        }

        private void Mytimer_tick(object sender, System.Timers.ElapsedEventArgs e)
        {
            this.Invoke(new SetControlValue(ShowTime), TimeCount);
            TimeCount++;
        }

        private void ShowTime(long t)
        {
            TimeSpan temp = new TimeSpan(0, 0, (int)t);
            txtTimeShow.Text = string.Format("{0:00}:{1:00}:{2:00}", temp.Hours, temp.Minutes, temp.Seconds);
        }




    }
}

 

 

 

 

实例二:倒计时功能

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 Test1
{
    public partial class FormTimer : Form
    {
        public FormTimer()
        {
            InitializeComponent();
        }

        int mint = 10;

        int scss = 59;

        private void button1_Click(object sender, EventArgs e)
        {
            label1.Text = mint + "分";

            label2.Text = scss + "秒";

            this.timer1.Interval = 1000; //设置间隔时间,为毫秒;
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);设置每间隔3000毫秒(3秒)执行一次函数timer1_Tick

            this.timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (mint >= 0)

            {

                scss--;

                if (scss == 0)

                {

                    mint--;

                    label1.Text = mint.ToString() + "分";

                    scss = 59;

                }

                label2.Text = scss.ToString() + "秒";

            }

        }



    }
}

 

 

  • 8
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值