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

    }

  • 相关阅读:
    java-集合框架-泛型2-泛型限定
    进程间通信
    多进程编程基础概念
    linux deb 打包流程
    linux RPM 打包流程
    Python 第一個程序
    从注册验证码入手,我保住了30%的流失用户
    为什么Web端登录需要验证码?
    网络验证码的进化:从简单图文到无感验证
    公开课 | 金融知识图谱的应用探索
  • 原文地址:https://www.cnblogs.com/BensonHai/p/5761774.html
Copyright © 2011-2022 走看看