using System.Runtime.InteropServices;
using Microsoft.Win32; [DllImport("CoreDll.dll")]
private static extern void SystemIdleTimerReset();
private static int nDisableSleepCalls = 0;
private static System.Threading.Timer preventSleepTimer = null;
private static void PokeDeviceToKeepAwake(object extra)
{
try
{
SystemIdleTimerReset();//调用系统函数,使系统不休眠
}
catch (Exception e)
{
// TODO
}
}
/**/
/// <summary>
/// </summary>
public static void DisableDeviceSleep()
{
nDisableSleepCalls++;
if (nDisableSleepCalls == 1)
{
//Debug.Assert(preventSleepTimer == null);
preventSleepTimer = new System.Threading.Timer
(new System.Threading.TimerCallback(PokeDeviceToKeepAwake),
null, 0, 30 * 1000);
}
}
private void FrmMain_Load(object sender, EventArgs e)
{
DisableDeviceSleep();//在程序创建时开启刷新线程 }