待机模式下的计时器会发生什么? [英] What happens to timer in standby mode?

查看:27
本文介绍了待机模式下的计时器会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Timers 命名空间中的 Timer.当 PC 进入睡眠或休眠状态时,计时器会发生什么变化?

I'm using Timer from Timers namespace. What happens to timer when PC goes to sleep or hibernates?

我已将计时器设置为延迟 6 小时.

I have timer set to 6 hours delay.

在这些情况下会发生什么.

What will happen in those situations.

1) 计时器从 0 小时开始并立即进入睡眠/休眠状态.然后 PC 在第 5 小时唤醒.我的计时器会在接下来的 1 小时或接下来的 6 小时后触发吗?

1) Timer starts at hour 0 and goes to sleep/hibernation immediately. Then PC wakes at hour 5. Will my timer fire after next 1 hour or after next 6 hours?

2) 计时器从 0 小时开始并立即进入睡眠/休眠状态.然后 PC 在 7 小时唤醒.我的计时器会在 PC 唤醒后立即触发还是会错过"那一次并在接下来的 5 小时内触发?它会从 PC 唤醒时还是从上一个错过"事件开始计数到下一个事件?

2) Timer starts at hour 0 and goes to sleep/hibernation immediately. Then PC wakes at hour 7. Will my timer fire as soon as PC wakes or will it "miss" that one time and fire in next 5 hours? Will it start counting till next event from time of PC waking or from previous "missed" event?

推荐答案

好的.我问了我的朋友,这是他的结果:

Ok. I asked my friend and this are his results:

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 开始计数到下一个事件.

So in short. After sleeping and waking timer checks if it has missed any event. If it missed one it will fire start counting till next event from 0.

这篇关于待机模式下的计时器会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆