借鉴帖子: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() + "秒";

            }

        }



    }
}

 

 

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐