zoukankan      html  css  js  c++  java
  • 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("执行开始");

    }

  • 相关阅读:
    Android学习笔记——启动Activity及Activity生命周期
    TransposonPSI——转座子分析的入门自学
    关于 GraPhlAn 的孤独自学
    Javascript 正则表达式 子表达式
    关于set,list,map的理解
    js关于日期的操作
    JDBC和JTA事务区别
    远程调试本地服务或程序
    Fragment的数据传递
    记录自己的第一篇博客
  • 原文地址:https://www.cnblogs.com/BensonHai/p/5761774.html
Copyright © 2011-2022 走看看