Awakening Machine Schedule

What is the best way to programmatically make a computer with Windows XP (or higher) wake up at a specific time. (Ideally, this is similar to how Media Center can automatically start recording a specific television program)

I have a Windows service (written in C #), and I would like this service to be able to force the machine on which it was located to start at given times.

Are there any BIOS settings or prerequisites (such as ACPI) that you need to configure to work correctly?

This unit will use a dialup or 3G modem, so unfortunately it cannot rely on Wake on LAN.

+12
source share
4 answers

You can use the expected timers to wake up from a pause or sleep state. From what I can find, it is impossible to wake up programmatically from the normal shutdown mode (soft off / S5), in this case you need to specify the WakeOnRTC alarm clock in the BIOS. To use pending timers from C #, you need pInvoke . Import Declarations:

public delegate void TimerCompleteDelegate(); [DllImport("kernel32.dll", SetLastError = true)] static extern IntPtr CreateWaitableTimer(IntPtr lpTimerAttributes, bool bManualReset, string lpTimerName); [DllImport("kernel32.dll", SetLastError = true)] static extern bool SetWaitableTimer(IntPtr hTimer, [In] ref long pDueTime, int lPeriod, TimerCompleteDelegate pfnCompletionRoutine, IntPtr pArgToCompletionRoutine, bool fResume); [DllImport("kernel32.dll", SetLastError = true)] public static extern bool CancelWaitableTimer(IntPtr hTimer); 

You can use these functions as follows:

 public static IntPtr SetWakeAt(DateTime dt) { TimerCompleteDelegate timerComplete = null; // read the manual for SetWaitableTimer to understand how this number is interpreted. long interval = dt.ToFileTimeUtc(); IntPtr handle = CreateWaitableTimer(IntPtr.Zero, true, "WaitableTimer"); SetWaitableTimer(handle, ref interval, 0, timerComplete, IntPtr.Zero, true); return handle; } 

You can then cancel the expected timer with CancelWaitableTimer , using the return handle as an argument.

Your program can sleep and sleep using pInvoke:

 [DllImport("powrprof.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool SetSuspendState(bool hibernate, bool forceCritical, bool disableWakeEvent); public static bool Hibernate() { return SetSuspendState(true, false, false); } public static bool Sleep() { return SetSuspendState(false, false, false); } 

Your system may not allow programs that allow your computer to enter sleep mode. You can call the following method to enable sleep mode:

 public static bool EnableHibernate() { Process p = new Process(); p.StartInfo.FileName = "powercfg.exe"; p.StartInfo.CreateNoWindow = true; p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; p.StartInfo.Arguments = "/hibernate on"; // this might be different in other locales return p.Start(); } 
+10
source

The task scheduler program in Win7, taskschd.msc (and I also believe XP) can be configured to wake up the system on different triggers. These triggers can be a schedule, time, event, etc.

At least in Win7, for this you need to set "Allow wake up timers" to "Enabled". This setting is found in ...

-> Control Panel \ Hardware and Sound \ Power Options
click "Change plan settings"
click "Edit advanced power setting"
expand - "Dream"
Expand - Allow Timers

+4
source

It is best to use Wake on LAN . This will require another machine to send a special kind of package to wake up your car.

This will not help if your computer is not connected to the network, or for some reason you should not have moved this logic to another machine. But it is useful for some configurations where you have several computers and you want to wake them up programmatically.

+1
source

Some machines have a BIOS alarm clock that can be configured to start the computer at a specific hour. It should be possible to program this watch, but I do not know the specific details.

Change I found this program that will allow you to set the time. This is in C, under Linux, but maybe this can give you some advice.

A warning though: before trying anything that changes the BIOS settings, be sure to write each one on the BIOS screen, because in case of a BIOS error, you may return to the factory by default, and you may need to configure it again as it is.

0
source

Source: https://habr.com/ru/post/1432395/


All Articles