C#中的计时器要怎么用,给个例子

来源:学生作业帮助网 编辑:作业帮 时间:2024/04/28 01:17:46

C#中的计时器要怎么用,给个例子
C#中的计时器要怎么用,给个例子

C#中的计时器要怎么用,给个例子
点击一个start的button开始计时,时间到时,就执行某个事件(theout),在这个例子里就show一下时间值.
System.Timers.Timer t = new System.Timers.Timer();//实例化Timer类
private void btnStart_Click(object sender, EventArgs e)
{
int intTime = Convert.ToInt32(cmbTimer.Text.ToString()) * 60 * 60 * 1000;
t.Interval = intTime;//设置间隔时间,为毫秒;
t.Elapsed += new System.Timers.ElapsedEventHandler(theout);//到达时间的时候执行事件;
t.AutoReset = true;//设置是执行一次(false)还是一直执行(true);
t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件;
}
public void theout(object source, System.Timers.ElapsedEventArgs e)
{
MessageBox.Show(e.SignalTime.ToString());
}