C# 定时器 Timers.Timer Forms.Timer

1.定义在System.Windows.Forms里

    Windows.Forms里面的定时器比较简单,只要把工具箱中的Timer控件拖到窗体上,然后设置一下事件和间隔时间等属性就可以了

//启动定时器

     private void button1_Click(object sender, EventArgs e)
        {
            timer1.Tick += new EventHandler(timer1_Tick);//执行的方法
            timer1.Enabled = true;//  获取或设置计时器是否正在运行。 如果计时器当前处于启用状态,则为 true
            timer1.Start();//开启
            label1.Text = "已开启";
            MessageBox.Show("开启成功");
        }

       void timer1_Tick(object sender, EventArgs e)
        {
            MessageBox.Show("执行开始");
        }
        private void button2_Click(object sender, EventArgs e)
        {
            timer1.Stop();//停止
            label1.Text = "已暂停";
            MessageBox.Show("暂停成功");
        }

 

3.定义在System.Timers.Timer类里

使用System.Timers.Timer类

System.Timers.Timer t = new System.Timers.Timer(10000);//实例化Timer类,设置间隔时间为10000毫秒;

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("执行开始");

}

转载于:https://www.cnblogs.com/BensonHai/p/5761774.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值