介绍两种Timer定时器的使用

第一种,

直接实例化Timer类,设置时间间隔,到达时间后执行想要执行的事件。代码示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;

namespace Timer
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a new Timer with Interval set to 10 seconds.
            System.Timers.Timer aTimer = new System.Timers.Timer(10000); //实例化Timer类,设置间隔时间为10000毫秒;
            aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent); //到达时间的时候执行事件
            // Only raise the event the first time Interval elapses.
            aTimer.AutoReset = true; //设置是执行一次(false)还是一直执行(true);
            aTimer.Enabled = true; //是否执行System.Timers.Timer.Elapsed事件;

            Console.WriteLine("Press \'q\' to quit the sample.");
            while (Console.Read() != 'q') ;
        }

        // Specify what you want to happen when the event is raised.
        private static void OnTimedEvent(object source, ElapsedEventArgs e)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

第二种,

使用让程序休眠的方法Thread.sleep()。 Thread.Sleep静态方法,使当前线程挂起指定的时间。没有多线程的话,程序只有一个主线程,就是使整个程序休眠。

程序首先创建了一个定时器,它将在创建1秒之后开始每隔1秒调用一次CheckStatus()方法,当调用5次以后,在CheckStatus()方法中修改了时间间隔为2秒,并且指定在10秒后重新开始。当计数达到10次,调用Timer.Dispose()方法删除了timer对象,主线程于是跳出循环,终止程序。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ThreadExample
{
    class TimerExampleState
    {
        public int counter = 0;
        public Timer tmr;
    }
    class Program
    {
        static void Main(string[] args)
        {
            TimerExampleState s = new TimerExampleState();

            //创建代理对象TimerCallback,该代理将被定时调用
            TimerCallback timerDelegate = new TimerCallback(CheckStatus);

            //创建一个时间间隔为1s的定时器
            Timer timer = new Timer(timerDelegate, s, 1000, 1000);
            // 第一个参数:指定了TimerCallback 委托,表示要执行的方法;
            // 第二个参数:一个包含回调方法要使用的信息的对象,或者为空引用;
            // 第三个参数:延迟时间——计时开始的时刻距现在的时间,单位是毫秒,指定为“0”表示//立即启动计时器;
            // 第四个参数:定时器的时间间隔——计时开始以后,每隔这么长的一段时间,TimerCallback所代表的方法将被调用一次,单位也是毫秒。指定 Timeout.Infinite 可以禁用定期终止。
            s.tmr = timer;

            //主线程停下来等待Timer对象的终止
            while (s.tmr != null)
                Thread.Sleep(0);
            Console.WriteLine("Timer example done.");
            Console.ReadLine();
        }
        //下面是被定时调用的方法
        static void CheckStatus(Object state)
        {
            TimerExampleState s = (TimerExampleState)state;
            s.counter++;
            Console.WriteLine("{0} Checking Status {1}.", DateTime.Now.TimeOfDay, s.counter);

            if (s.counter == 5)
            {
                //使用Change方法改变了时间间隔
                (s.tmr).Change(10000, 2000);
                Console.WriteLine("changed");
            }

            if (s.counter == 10)
            {
                Console.WriteLine("disposing of timer");
                s.tmr.Dispose();
                s.tmr = null;
            }
        }
    }
}

运行的结果如下图:

 

posted on 2013-06-19 14:50  行意  阅读(1139)  评论(1编辑  收藏  举报

导航