Windows操作系统能在多长时间内从唤醒定时器中唤醒?

时间:2012-09-05 01:28:19

标签: c++ winapi sleep power-management wakeup

我正在使用C ++和Win API编写程序。我使用SetSuspendState()API将系统发送到睡眠模式(有可能在唤醒定时器上唤醒,'DisableWakeEvent'设置为FALSE。)然后我使用CreateWaitableTimer和SetWaitableTimer API来设置实际的定时器。问题是,如果在系统进入睡眠模式后过早设置唤醒定时器,系统有时不会唤醒。

所以我很好奇,如果系统被发送到睡眠模式之前必须通过最短时间才能以编程方式唤醒唤醒定时器?

1 个答案:

答案 0 :(得分:1)

现在。您的PC可以使用计时器唤醒:

Schedule machine to wake up

C#文章,如果你不介意:http://www.codeproject.com/Articles/49798/Wake-the-PC-from-standby-or-hibernation

using System;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
using System.ComponentModel;
using System.Threading;

namespace WakeUPTimer
{
    class WakeUP
    {
        [DllImport("kernel32.dll")]
        public static extern SafeWaitHandle CreateWaitableTimer(IntPtr lpTimerAttributes, 
                                                                  bool bManualReset,
                                                                string lpTimerName);

        [DllImport("kernel32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool SetWaitableTimer(SafeWaitHandle hTimer, 
                                                    [In] ref long pDueTime, 
                                                              int lPeriod,
                                                           IntPtr pfnCompletionRoutine, 
                                                           IntPtr lpArgToCompletionRoutine, 
                                                             bool fResume);

        public event EventHandler Woken;

        private BackgroundWorker bgWorker = new BackgroundWorker();

        public WakeUP()
        {
            bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
            bgWorker.RunWorkerCompleted += 
              new RunWorkerCompletedEventHandler(bgWorker_RunWorkerCompleted);
        }

        public void SetWakeUpTime(DateTime time)
        {
            bgWorker.RunWorkerAsync(time.ToFileTime());
        }

        void bgWorker_RunWorkerCompleted(object sender, 
                      RunWorkerCompletedEventArgs e)
        {
            if (Woken != null)
            {
                Woken(this, new EventArgs());
            }
        }

        private void bgWorker_DoWork(object sender, DoWorkEventArgs e) 
        {
            long waketime = (long)e.Argument;

            using (SafeWaitHandle handle = 
                      CreateWaitableTimer(IntPtr.Zero, true, 
                      this.GetType().Assembly.GetName().Name.ToString() + "Timer"))
            {
                if (SetWaitableTimer(handle, ref waketime, 0, 
                                       IntPtr.Zero, IntPtr.Zero, true))
                {
                    using (EventWaitHandle wh = new EventWaitHandle(false, 
                                                           EventResetMode.AutoReset))
                    {
                        wh.SafeWaitHandle = handle;
                        wh.WaitOne();
                    }
                }
                else
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }
            }
        }

    }
}

或从控制台:http://www.anuko.com/content/world_clock/faq/enable_wake_timers.htm