WPF Application: Wake computer from sleep or hibernation

和自甴很熟 提交于 2021-02-19 08:43:12

问题


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:


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



来源:https://stackoverflow.com/questions/10716206/wpf-application-wake-computer-from-sleep-or-hibernation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!