行。我问了我的朋友,这是他的结果:
23:21:32 : Timer started
23:21:35 : PC Goes Sleep
23:22:50 : PC Wakes
23:22:50 : Timer fired
23:23:50 : Timer fired
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace Test
{
class Program
{
static System.Timers.Timer timer;
static void Main(string[] args)
{
timer = new System.Timers.Timer();
timer.Interval = 60 * 1000;
timer.AutoReset = true;
timer.Elapsed += timer_Elapsed;
timer.Enabled = true;
Console.WriteLine(String.Format("{0}:{1}:{2} : Timer started", DateTime.Now.ToLocalTime().Hour, DateTime.Now.ToLocalTime().Minute, DateTime.Now.ToLocalTime().Second));
timer.Start();
Thread.Sleep(Timeout.Infinite);
}
static void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
Console.WriteLine(String.Format("{0}:{1}:{2} : Timer fired", DateTime.Now.ToLocalTime().Hour, DateTime.Now.ToLocalTime().Minute, DateTime.Now.ToLocalTime().Second));
}
}
}
所以简而言之。睡眠和唤醒计时器后检查它是否错过了任何事件。如果它错过了一个,它将触发从 0 开始计数直到下一个事件。