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

    }

  • 相关阅读:
    爱情的诗·1~5节
    人生的诗·381~385节
    人生的诗·375~380节
    python数据类型初始1
    python编码知识初始_ASCII码,Unicode,Utf-8,GBK
    Python运算符,逻辑运算
    python格式化输出%,while else
    pycharm使用教程链接+部分练习题01
    python流程控制-条件语句If,while循环
    python基础数据类型初始,用户交互
  • 原文地址:https://www.cnblogs.com/BensonHai/p/5761774.html
Copyright © 2011-2022 走看看