码迷,mamicode.com
首页 > Windows程序 > 详细

C# 定时器 Timers.Timer Forms.Timer

时间:2016-08-11 17:47:20      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:

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

}

C# 定时器 Timers.Timer Forms.Timer

标签:

原文地址:http://www.cnblogs.com/BensonHai/p/5761774.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!