C# Timer,System.Threading.Timer, System.Timers.Timer

 static void Main(string[] args)
        {
            SystemThreadingTimerDemo();
            Console.ReadLine();
        }

        static void SystemThreadingTimerDemo()
        {
            System.Threading.Timer timer = new Timer(TimerCB2, 0, 0, 10000);           
        }

        private static void TimerCB2(object state)
        {            
            NumIncrement();
        }       

        static void SystemTimersTimerDemo()
        {
            System.Timers.Timer timer = new System.Timers.Timer();
            timer.Interval = 10000;
            timer.Elapsed += TimerElapsed;
            timer.Start();
        }

        private static void TimerElapsed(object sender, System.Timers.ElapsedEventArgs e)
        {          
            NumIncrement();
        }

        public static void NumIncrement()
        {
            Num = 0;
            DateTime dt = DateTime.Now;
            DateTime endDt = dt.AddSeconds(10);
            while (DateTime.Now < endDt)
            {
                Num++;
                Console.WriteLine($"{DateTime.Now.ToString("yyyyMMddHHmmssffff")},Num={Num}");
                Thread.Sleep(1000);
            }
        }

 

posted @ 2020-03-11 14:27  FredGrit  阅读(373)  评论(0编辑  收藏  举报