【C#】定时器的使用

一、在窗体类public partial class Form1 : Form下声明:

        private System.Windows.Forms.Timer timerGetTime;

二、开启定时器

private void button4_Click(object sender, EventArgs e)
{
    
     //创建定时器
     timerGetTime = new System.Windows.Forms.Timer();
        
     //设置定时器属性
     timerGetTime.Tick += new EventHandler(HandleTime);
     timerGetTime.Interval = 1000;//时间间隔1000毫秒
     timerGetTime.Enabled = true;
        
     //开启定时器
     timerGetTime.Start();
        
}

定时器函数

public void HandleTime(Object myObject, EventArgs myEventArgs)      
{

     //加入需要执行的函数,即,每隔1000毫秒执行一次该函数

}

三、停止定时器

private void button5_Click(object sender, EventArgs e)
{

    //停止定时器
    timerGetTime.Stop();
    
}
发布了74 篇原创文章 · 获赞 24 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_43197380/article/details/103886893