首页 > 其他 > 详细

定时器&改变定时器的执行频率

时间:2018-08-27 15:19:54      阅读:209      评论:0      收藏:0      [点我收藏+]

技术分享图片

 

 static System.Threading.Timer timer; 
        static void Main(string[] args)
        {
            Console.WriteLine("Press Enter key to stop timer");
            DateTime start = DateTime.Now;
            //启动定时器,每2秒执行一次
            timer = new Timer(p => TimerOperation(start),null,TimeSpan.FromSeconds(1),TimeSpan.FromSeconds(2));
            //休眠10秒,10/2=5,即定时器执行5次
            Thread.Sleep(TimeSpan.FromSeconds(10));
            //在修改为调用change方法1秒后启动TimerOperation,然后每5秒执行一次
            timer.Change(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(5));
            Console.ReadLine();
            timer.Dispose();
        }

        static void TimerOperation(DateTime start)
        {
            TimeSpan elapsed = DateTime.Now - start;
            Console.WriteLine("{0} seconds from {1}. timer thead pool thread id : {2}",elapsed.Seconds,start,Thread.CurrentThread.ManagedThreadId);

        }

技术分享图片

定时器&改变定时器的执行频率

原文:https://www.cnblogs.com/sportdog/p/9542326.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!