1

我试图完成这项工作的最后一天:使用 WPF 应用程序将我的计算机从睡眠或休眠状态唤醒。我试过的都没有用。

到目前为止,我已经尝试了网络上最流行的示例。例如:

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

以及用法:

WakeUp.SetWaitForWakeUpTime();

我也试过这个例子(看看我如何在代码之后使用该方法):

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

}

以及用法:

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

似乎没有什么能唤醒我的电脑。

我知道这是可能的,因为其他几个闹钟可以做到这一点。他们设法毫无问题地唤醒我的电脑,所以一定有一些错误。

4

1 回答 1

1

问题不是所有的计算机都支持这个问题。如果您的计算机不支持它,显然没有什么可做的。

于 2012-07-14T20:50:43.703 回答