C#实现定时器
方法一
布局
代码
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 会变的数1
{public partial class Form1 : Form{public Form1(){InitializeComponent();}private System.Windows.Forms.Timer timerGetTime;private void Form1_Load(object sender, EventArgs e){//创建定时器timerGetTime = new System.Windows.Forms.Timer();//设置定时器属性timerGetTime.Tick += new EventHandler(HandleTime);timerGetTime.Interval = 1000;timerGetTime.Enabled = true;//开启定时器timerGetTime.Start();}public void HandleTime(Object myObject, EventArgs myEventArgs){label1.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");}private void Form1_FormClosed(object sender, FormClosedEventArgs e){//停止定时器timerGetTime.Stop();}}
}
效果
使用time控件
布局
代码
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 time1
{public partial class Form1 : Form{//定义全局变量public int currentCount = 0;public Form1(){InitializeComponent();}private void Form1_Load(object sender, EventArgs e){//设置time控件可用this.timer1.Enabled = true;//设置时间间隔(毫秒为单位)this.timer1.Interval = 1000;//默认状态时停止计时状态this.timer1.Stop();}//点击定时器就会出现该函数private void timer1_Tick(object sender, EventArgs e){currentCount += 1;this.label1.Text = currentCount.ToString().Trim();}private void button1_Click(object sender, EventArgs e){this.timer1.Start();}private void button2_Click(object sender, EventArgs e){this.timer1.Stop();}}
}
效果
写作技巧
开关放函数外面,时间结束后进行的动作放在函数内。
System.Timers.Timer
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 time2
{public partial class Form1 : Form{//定义全局变量public int currentCount = 0;//定义timer类System.Timers.Timer timer;//定义委托public delegate void SetControlVlue(string value);public Form1(){InitializeComponent();}private void Form1_Load(object sender, EventArgs e){InitTimer();timer.Stop();}//初始化timer控件private void InitTimer(){//设置定时间隔(毫秒为单位)int interval = 1000;timer = new System.Timers.Timer(interval);//设置执行一次(false)还是一直执行(true)timer.AutoReset = true;//设置是否执行System.Timers.Timer.Elapsed事件timer.Enabled = true;//绑定Elapsed事件timer.Elapsed += new System.Timers.ElapsedEventHandler(TimerUp);}// Timer类执行定时到点事件private void TimerUp(object sender, System.Timers.ElapsedEventArgs e){try{currentCount += 1;this.Invoke(new SetControlVlue(SetLabelText), currentCount.ToString());}catch (Exception ex){throw; MessageBox.Show("执行定时到点事件失败:" + ex.Message);}}//设置标签的文本值private void SetLabelText(string strValue){this.label1.Text = this.currentCount.ToString().Trim();}private void button1_Click(object sender, EventArgs e){timer.Start();}private void button2_Click(object sender, EventArgs e){timer.Stop();}}
}
System.Threading.Timer
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;
using System.Threading;//使用系统的线程空间namespace time3
{public partial class Form1 : Form{//定义全局变量public int currentCount = 0;//定义timer类System.Threading.Timer threadTimer;//定义委托public delegate void SetControlValue(object value);public Form1(){InitializeComponent();}private void Form1_Load(object sender, EventArgs e){InitTimer();}/// 初始化Timer类private void InitTimer(){threadTimer = new System.Threading.Timer(new TimerCallback(TimerUp), null, Timeout.Infinite, 1000);}/// 定时到点执行的事件private void TimerUp(object value){currentCount += 1;this.Invoke(new SetControlValue(SetTextBoxValue), currentCount);}/// <summary>/// 给文本框赋值/// </summary>/// <param name="value"></param>private void SetTextBoxValue(object value){this.label1.Text = value.ToString();}private void button1_Click(object sender, EventArgs e){//立即开始计时,时间间隔1000毫秒threadTimer.Change(0, 1000);}private void button2_Click(object sender, EventArgs e){//停止计时threadTimer.Change(Timeout.Infinite, 1000);}}}