Możesz użyć waitable timers, aby wybudzić ze stanu zawieszenia lub hibernacji. Z tego, co udało mi się znaleźć, nie można programowo wybudzić z normalnego trybu wyłączania (miękkie wyłączanie/S5), w takim przypadku należy określić alarm WakeOnRTC w systemie BIOS. Aby używać wyczekiwanych timerów z C#, potrzebujesz pInvoke. Deklaracje importowe są:
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);
Można korzystać z tych funkcji w następujący sposób:
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;
}
Następnie można anulować waitable czasomierz z CancelWaitableTimer
korzystając zwrócony uchwyt jako argument.
Program może hibernacji i uśpienia przy użyciu 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);
}
Twój system może nie pozwolić programy, aby komputer do stanu hibernacji. Możesz wywołać następującą metodę zezwalającą na hibernację:
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();
}
Może ludzie na stronie http://serverfault.com będą o tym wiedzieć. – jmservera