Mock heartbeat via While true Thread.Sleep and System.Timers.Timer

static void MockHeartBeatViaWhileTrue()
        {
            while (true)
            {
                Console.WriteLine($"Now is {DateTime.Now.ToString("yyyyMMddHHmmssffff")}");
                Thread.Sleep(2000);
            }
        }
 static void HeartBeatViaSystemTimersTimerDemo()
        {
            System.Timers.Timer heartBeatTimer = new System.Timers.Timer(2000);
            heartBeatTimer.Elapsed += HeartBeatTimerElapsed;
            heartBeatTimer.Start();

        }

        private static void HeartBeatTimerElapsed(object sender, ElapsedEventArgs e)
        {
            Console.WriteLine($"Now is {DateTime.Now.ToString("yyyyMMddHHmmssffff")}");
        }

 

posted @ 2020-05-17 12:24  Fred1987  阅读(71)  评论(0编辑  收藏