最后一天我试图使这个工作:用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)));
似乎没什么能唤醒我的电脑
我知道这是可能的,正如其他几个闹钟可以做到的那样。他们设法使我的电脑醒过来,没有问题,所以一定有错误。