2012-05-23 146 views
1

我試圖完成這項工作的最後一天:使用WPF應用程序將我的計算機從睡眠或休眠狀態喚醒。沒有我試過的工作。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))); 

一切似乎喚醒我的電腦。

我知道這是可能的,因爲其他幾個鬧鐘都可以。他們設法沒有問題地喚醒我的電腦,所以一定有一些錯誤。

+0

參考此鏈接http://social.msdn.microsoft.com/Forums/da-DK/csharpgeneral/thread/89002ed0-21e6-4429-ac9d-89ff5faed164 – JSJ

+0

相同的機制,因爲我使用,不工作..啊啊! –

回答

1

問題不是所有的計算機都支持這個問題。如果你的電腦不支持它,顯然沒有多少事要做。

+0

支持什麼問題?通過用戶模式進程喚醒? –

相關問題