0

Hi everybody my application needs to put PC to the Sleep mode and Wake it Up after certain period. To investigate this case i made simple Console application to put PC Sleep and wake it up after 10 seconds .

after a long research on google and here on Stack i found that generally all answers goes to this sample: http://www.anotherchris.net/csharp/wake-up-from-sleep-createwaitabletimer-in-csharp/

1)I tried it on one pc (windows 7) and it went to sleep but did not wake up 2) now trying to make it work on another PC (windows 10) but it even does not get to the end of the function...- probably stays waiting at : wh.waitOne line ...

so here is my code- what am i doing wrong?!?- will appreciate any help.... or suggestions on another way doing it ...

using System;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
using System.Threading;
using System.ComponentModel;
using System.Windows.Forms;
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();
            Application.SetSuspendState(PowerState.Suspend, false, false);

        }

        static void SetWaitForWakeUpTime()
        {
            DateTime utc = DateTime.Now.AddSeconds(5);
            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();
        }
    }
} 
Johan
  • 8,068
  • 1
  • 33
  • 46
Kamornik Cola
  • 644
  • 8
  • 20

3 Answers3

1

I used the Task Scheduler Managed Wrapper. It was a great solution for my problem:

Make a scheduled task to fire after a period of time, it wakes the PC from sleep. I made a small "cmd.exe" execution script with "exit" in it to schedule nothing.

I used: https://taskscheduler.codeplex.com/

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Kamornik Cola
  • 644
  • 8
  • 20
  • You might want to use [rundll32.exe with no parameters](http://superuser.com/questions/381103/is-there-a-windows-exe-that-does-nothing) instead. – Scott Chamberlain Nov 05 '15 at 21:02
0

There is a project over on CodeProject that give an example of doing this: http://www.codeproject.com/Articles/49798/Wake-the-PC-from-standby-or-hibernation

However, I've seen in comments elsewhere that not all motherboards support the feature utilized in that application, so it might work on some, but not all computers.

Jerren Saunders
  • 1,188
  • 1
  • 8
  • 26
  • i've seen it - it makes exactly the same code... just started from the different thread .... there is a program: http://www.passmark.com/products/sleeper.htm that works perfectly on my PC - and makes exactly this...- i'm trying to do the same.... – Kamornik Cola Nov 03 '15 at 18:32
  • I'm curious if their program is programmatically using Task Scheduler to schedule a task that wakes the PC. http://stackoverflow.com/a/3764944 – Jerren Saunders Nov 03 '15 at 22:28
0

Duetime should be negative and expressed in hundreds of nanoseconds. The following example will wake up the computer in 30 seconds.

long duetime = -300000000; 

msdn https://msdn.microsoft.com/en-us/library/windows/desktop/ms686289(v=vs.85).aspx

Michal
  • 119
  • 1
  • 10