Created
February 19, 2013 14:31
-
-
Save yetanotherchris/4986402 to your computer and use it in GitHub Desktop.
Wake up from sleep C# example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Runtime.InteropServices; | |
using Microsoft.Win32.SafeHandles; | |
using System.Threading; | |
using System.ComponentModel; | |
namespace ConsoleApplication1 | |
{ | |
class Program | |
{ | |
[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); | |
static void Main(string[] args) | |
{ | |
SetWaitForWakeUpTime(); | |
} | |
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(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code looks similar to code presented at https://www.codeproject.com/Articles/49798/Wake-the-PC-from-standby-or-hibernation. I'm not sure who copied who. The DiSarli code succeeds in waking my computer from sleep while the code here doesn't. The code here just runs to completion after the wakeup timer expires if the computer is not sleeping. The big difference between the two code sets is the DiSarli code uses a BackgroundWorker object for reasons he explains in his article: "So, to avoid blocking the UI thread, we need to put that block of code into a separate thread, controlled by a BackgroundWorker object to which we'll pass the wake time as an argument." I really don't see the relevance of this statement. It's obviously the key to success. Can anyone enlighten me?
Update after additional research and testing: I believe this code works 100%. There is a minimum sleep time needed before this wake-up application can truly wake up the computer. I was using 20 seconds for testing (a low number gives me more testing iterations per unit time). Certain web comments stated a minimum time was needed but no specific numbers were suggested. I changed my testing to 120 seconds and now it works every time. I believe the BackgroundWorker in the DiSarli code is not needed because all the code can run in the same thread. I believe this code represents the minimum amount of C# code needed to wake up.