我使用 C# 创建了一个 Windows 服务,需要添加一个计时器,在此期间服务将处于空闲状态,并且应该仅在空闲超时发生后才开始处理。
如果设备进入休眠状态,计时器会重新开始还是继续?
最佳答案
做了一点体操并确定了一个我认为可行的解决方案
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Diagnostics;
namespace Test
{
class Program
{
static void Main(string[] args)
{
int maxtimeout = 100; // max timeout is 100 seconds, when it reaches this break
var prevtime = ((DateTime.Now - DateTime.MinValue).TotalMilliseconds) / 1000;
Console.WriteLine("current time = {0} seconds", prevtime);
for (; ; )
{
Thread.Sleep(5000);
var newtime = ((DateTime.Now - DateTime.MinValue).TotalMilliseconds) / 1000;
var timeelapsed = newtime - prevtime;
Console.WriteLine("Time elapsed = {0}", timeelapsed);
if (timeelapsed >= maxtimeout)
{
Console.WriteLine("Max timeout reached");
break;
}
else
{
Console.WriteLine("Max timeout no reached elapased time = {0}", timeelapsed);
}
}
}
}
}
关于c# - 设备进入休眠状态后 C# 计时器是否继续计数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28146176/