È possibile utilizzare waitable timers di svegliarsi da uno stato di sospensione o ibernazione. Da quello che posso trovare, non è possibile riattivare a livello di programmazione dalla normale modalità di spegnimento (soft off/S5), in tal caso, è necessario specificare un allarme WakeOnRTC nel BIOS. Per utilizzare i timer attendibili da C#, è necessario pInvoke. Le dichiarazioni di importazione sono:
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);
È possibile utilizzare queste funzioni nel modo seguente:
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;
}
si può quindi annullare il timer waitable con CancelWaitableTimer
, utilizzando l'handle restituito come argomento.
Il programma può hibernate e dormire con 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);
}
Il vostro sistema potrebbe non consentire ai programmi per lasciare che il computer può entrare in modalità di sospensione. È possibile chiamare il seguente metodo per consentire la sospensione:
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();
}
Forse le persone su http://serverfault.com lo saprebbero. – jmservera