4

I'm writing a program using C++ and Win APIs. I employed SetSuspendState() API to send the system into a sleep mode (with a possibility to wake on a wake timer, 'DisableWakeEvent' set to FALSE.) I then use CreateWaitableTimer and SetWaitableTimer API to set the actual timer. The issue is that sometimes the system does not wake up if I set the wake timer too soon after the system enters the sleep mode.

So I was curious if there's a minimum time that has to pass since the system is sent into a sleep mode before it can be woken up with a wake timer programmatically?

4
  • 1
    My 100% guess is that it depends on the actual machine. Just picking a reasonable value might be the best you can do...
    – user541686
    Sep 5, 2012 at 1:32
  • I'm sorry, but what's the reasonable value?
    – c00000fd
    Sep 5, 2012 at 2:29
  • That's left as an exercise for the reader. ;) I think ~2 minutes should cover most cases.
    – user541686
    Sep 5, 2012 at 2:30
  • Reading the documentation to which you provided the link, it appears that the ForceCritical parameter can influence how long it may take to suspend operation. If the parameter is FALSE then the system broadcasts an event to each application to request permission to suspend so if there are applications running that take time to respond, will that influence how long it takes to suspend? Sep 15, 2012 at 3:10

1 Answer 1

1

Right now. Your PC can wake up using a timer:

Schedule machine to wake up

C# Article if you dont mind : 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());
                }
            }
        }

    }
}

Or from the control panel : http://www.anuko.com/content/world_clock/faq/enable_wake_timers.htm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.