zoukankan      html  css  js  c++  java
  • C# 计时器的三种使用方法

    在.net中有三种计时器,一是System.Windows.Forms命名空间下的Timer控件,它直接继承自Componet;二是System.Timers命名空间下的Timer类。

    Timer控件:Timer控件只有绑定了Tick事件,和设置Enabled=True后才会自动计时,停止计时可以用Stop()控制,通过Stop()停止之后,如果想重新计时,可以用Start()方法来启动计时器。Timer控件和它所在的Form属于同一个线程;

    System.Timers.Timer类:定义一个System.Timers.Timer对象,绑定Elapsed事件,通过Start()方法启动计时,通过Stop()方法或者Enable=False停止计时。AutoReset属性设置是否重复计时。Elapsed事件绑定就相当另开了一个线程,也就是说在Elapsed绑定的事件里不能访问其它线程里的控件。

    System.Threading.Timer:定义该类时,主要有四个参数。TimerCallBack,一个返回值为void,参数为object的委托,也是计时器执行的方法。Object state,计时器执行方法的的参数。 int dueTime,调用 callback 之前延迟的时间量(以毫秒为单位)。指定 Timeout.Infinite 以防止计时器开始计时。指定零 (0) 以立即启动计时器。

    int Period,调用 callback 的时间间隔(以毫秒为单位)。指定 Timeout.Infinite 可以禁用定期终止。

    在这三种计时器中,第一种计时器和所在的Form处于同一个线程,因此执行的效率不高。而第二种和第三中计时器执行的方法都是新开一个线程,所以执行效率比第一种计时器要好。因此在使用计时器时,建议使用第二种和第三种。

    下面是三中定时器使用的例子

    1)Timer控件  

      public partial class Timer : Form
        {

            int count = 0;
            public Timer()
            {
                InitializeComponent();

                //timer控件可用
                this.timer1.Enabled = true;

               //设置timer控件的Tick事件触发的时间间隔
                this.timer1.Interval = 1000;

                //停止计时
                this.timer1.Stop();
            }

            private void timer1_Tick(object sender, EventArgs e)
            {
                count += 1;
                this.tbTimer.Text = count.ToString();
            }

            private void btStart_Click(object sender, EventArgs e)
            {
                //开始计时
                this.timer1.Start();
            }

            private void btStop_Click(object sender, EventArgs e)
            {
                //停止计时
                this.timer1.Stop();
                
            }

        }

    2)System.Timers.Timer

    public partial class Timer : Form
        {
            int count = 0;

            private System.Timers.Timer timer = new System.Timers.Timer();

            public Timer()
            {
                InitializeComponent();

                //设置timer可用
                timer.Enabled = true;
                
                //设置timer
                timer.Interval = 1000;

                //设置是否重复计时,如果该属性设为False,则只执行timer_Elapsed方法一次。
                timer.AutoReset = true;

                timer.Elapsed+=new System.Timers.ElapsedEventHandler(timer_Elapsed);
            }

            private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) 
            {
                count += 1;
                SetTB(count.ToString());
            }

            private void btStart_Click(object sender, EventArgs e)
            {
                timer.Start();
            }

            private void btStop_Click(object sender, EventArgs e)
            {
                timer.Stop();
            }

            private delegate void SetTBMethodInvok(string value);

            private void SetTB(string value)
            {
                if (this.InvokeRequired) 
                {
                    this.Invoke(new SetTBMethodInvok(SetTB), value);
                }
                else
                {
                    this.tbTimer.Text = value;
                }
            }
        }

    3) System.Threading.Timer

        public partial class Timer : Form
        {
            int count = 0;
            System.Threading.Timer timerThr;
            private delegate void SetTBMethodInvoke(object state);

            public Timer()
            {
                InitializeComponent();

                //初始化一个计时器,一开始不计时,调用Callback的时间间隔是500毫秒
                timerThr = new System.Threading.Timer(new TimerCallback(SetTB), null, Timeout.Infinite, 500);
            }

            public void SetTB(object value)
            {
                if (this.InvokeRequired)
                {
                    this.Invoke(new SetTBMethodInvoke(SetTB), value);
                }
                else 
                {
                    count += 1;
                    this.tbTimer.Text = count.ToString();
                }
            }

            private void btStart_Click(object sender, EventArgs e)
            {
                //开始计时
                timerThr.Change(0, 500);
            }

            private void btStop_Click(object sender, EventArgs e)
            {
                //停止计时
                timerThr.Change(Timeout.Infinite, 500);
            }
        }

  • 相关阅读:
    [转]使用.NET中的XML注释(一) XML注释标签讲解
    Cookie文件格式
    [转]去掉网页上链接或按钮的虚线框
    [转]TFS提供修改密码的页面
    [转]动态操作Stylesheet里的rule
    模拟TcpClient的Timeout
    [转]使用.NET中的XML注释(二) 创建帮助文档入门篇
    【转】PowerPoint 2007与微软拼音输入法的问题
    [转]BloomFilter——大规模数据处理利器
    关于费马小定理与欧拉定理的关系
  • 原文地址:https://www.cnblogs.com/1175429393wljblog/p/5810238.html
Copyright © 2011-2022 走看看