WPF Application: Wake computer from sleep or hibernation

2k views Asked by At

The last day i've tried to make this work: Wake up my computer from sleep or hibernation, using a WPF application. Nothing i've tried worked.

So far I've tried the most popular examples on the net.. For example:

[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 static void SetWaitForWakeUpTime()
        {
            DateTime utc = DateTime.Now.AddMinutes(2);
            long duetime = utc.ToFileTime();

            using (SafeWaitHandle handle = CreateWaitableTimer(IntPtr.Zero, true, "MyWaitabletimer"))
            {
                if (SetWaitableTimer(handle, ref duetime, 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());
                }
            }

            // You could make it a recursive call here, setting it to 1 hours time or similar
            Console.WriteLine("Wake up call");
            Console.ReadLine();
        }

And the usage:

WakeUp.SetWaitForWakeUpTime();

I've also tried this example (see how I use the method after the code):

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());
        }
    }
}

}

And the usage:

WakeUp w = new WakeUp();
                w.Woken += new EventHandler(w_Woken);
                w.SetWakeUpTime(alarmDate.Subtract(new TimeSpan(0,0,0,20)));

Nothing seems to wake up my PC.

I know it is possible, as several other alarm clocks can do it. They manage to wake up my computer with no problems, so there must be some mistake.

1

There are 1 answers

1
Lars Holdgaard On BEST ANSWER

The problem was not all computers support this issue. There is apparently not much to do if your computer doesn't support it.