Timer控件只有绑定了Tick事件和设置Timer.Enabled = ture后才会自动计时,停止计时可以用Stop()方法控制,通过Stop()方法来启动计时器。Timer控件和它所在的Form属于同一个线程;
System.Timers.Timer类:定义一个System.Timers.Timer对象,然后绑定Elapsed事件,通过Start()方法来启动,通过Stop()方法或者Enadled = false停止计时。AutoReset属性是用来设置是否重复计时(设置为false只执行一次,设置为true可以多次执行)。在Elapsed事件中绑定相当于另开了一个线程,也就是说在Elapsed绑定的事件里不能访问其他线程里的控件,但是可以通过定义委托,通过Invoke调用委托访问其他线程里面的控件。
定义该类时需通过构造函数进行初始化。
界面:
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace TimerDemo
{
public partial class FrmMain : Form
{
//定义全局变量
public int currentCount = 0;
public FrmMain()
{
InitializeComponent();
}private void FrmMain_Load(object sender, EventArgs e)
{
//设置Timer控件可用
this.timer.Enabled = true;
//设置时间间隔(毫秒为单位)
this.timer.Interval = 1000;
}private void timer_Tick(object sender, EventArgs e)
{
currentCount += 1;
this.txt_Count.Text = currentCount.ToString().Trim();
}private void btn_Start_Click(object sender, EventArgs e)
{
//开始计时
this.timer.Start();
}private void btn_Stop_Click(object sender, EventArgs e)
{
//停止计时
this.timer.Stop();
}
}
}
设计界面:
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace TimersTimer
{
public partial class FrmMain : Form
{
//定义全局变量
public int currentCount = 0;
//定义Timer类
System.Timers.Timer timer;
//定义委托
public delegate void SetControlValue(string value);public FrmMain()
{
InitializeComponent();
}private void Form1_Load(object sender, EventArgs e)
{
InitTimer();
}///
/// 初始化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 SetControlValue(SetTextBoxText),currentCount.ToString());
}
catch (Exception ex)
{
MessageBox.Show("执行定时到点事件失败:" + ex.Message);
}
}///
/// 设置文本框的值
///
///
private void SetTextBoxText(string strValue)
{
this.txt_Count.Text = this.currentCount.ToString().Trim();
}private void btn_Start_Click(object sender, EventArgs e)
{
timer.Start();
}private void btn_Stop_Click(object sender, EventArgs e)
{
timer.Stop();
}
}
}
设计界面:
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace Threading.Timer
{
public partial class FrmMain : Form
{
//定义全局变量
public int currentCount = 0;
//定义Timer类
System.Threading.Timer threadTimer;
//定义委托
public delegate void SetControlValue(object value);public FrmMain()
{
InitializeComponent();
}private void FrmMain_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);
}///
/// 给文本框赋值
///
///
private void SetTextBoxValue(object value)
{
this.txt_Count.Text = value.ToString();
}///
/// 开始
///
///
///
private void btn_Start_Click(object sender, EventArgs e)
{
//立即开始计时,时间间隔1000毫秒
threadTimer.Change(0, 1000);
}///
/// 停止
///
///
///
private void btn_Stop_Click(object sender, EventArgs e)
{
//停止计时
threadTimer.Change(Timeout.Infinite, 1000);
}
}
}