c# - 设备进入休眠状态后 C# 计时器是否继续计数?

标签 c# windows service timer

我使用 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/

相关文章:

c# - 什么是阅读 ISomething : ISomethingElse 的正确方法

c# - UIViewController 子类(模仿 UITableViewController)未发布

来自 VS2005 的 C++ 编译器

c# - Visual Studio 无法识别版本 10.0.14393.0 中的 Windows 10 SDK

java服务与资源共享

c# - 线程计时器不起作用?

c# - MediatR 共享 TableBatchOperation 以在不同的处理程序中进行事务保存

java - 从 Java 调用时隐藏 BAT 文件窗口

docker - Kubernetes 服务连接被拒绝,如何访问服务?

c++ - Windows 10 中的 Windows 服务安装/卸载