怎样使软件在运行的时候系统不进入待机状态解决思路
怎样使软件在运行的时候系统不进入待机状态
RT
查了网上的资料,说可以定时的使用SystemIdleTimerReset();但是怎么样获取这个时间频率呢,或者说如何获取系统从无输入情况下到进入待机时间。。或者有什么更好的方法呢。。高手答疑下。。。
------解决方案--------------------
我觉得启动个Timer,每30秒调用SystemIdleTimerReset一次,对系统和程序性能影响都不大。
------解决方案--------------------
查过注册表,里面有时间!
就是系统设置中的值在注册表里面有。
------解决方案--------------------
/// <summary>
/// This function resets a system timer that controls whether or not the
/// device will automatically go into a suspended state.
/// </summary>
[DllImport("CoreDll.dll")]
public static extern void SystemIdleTimerReset();
private static int nDisableSleepCalls = 0;
private static System.Threading.Timer preventSleepTimer = null;
public static void DisableDeviceSleep()
{
nDisableSleepCalls++;
if (nDisableSleepCalls == 1)
{
Debug.Assert(preventSleepTimer == null);
// start a 30-second periodic timer
preventSleepTimer = new System.Threading.Timer(new TimerCallback(PokeDeviceToKeepAwake),
null, 0, 30 * 1000);
}
}
public static void EnableDeviceSleep()
{
nDisableSleepCalls--;
if (nDisableSleepCalls == 0)
{
Debug.Assert(preventSleepTimer != null);
if (preventSleepTimer != null)
{
preventSleepTimer.Dispose();
preventSleepTimer = null;
}
}
}
private static void PokeDeviceToKeepAwake(object extra)
{
try
{
PlatformUtil.SystemIdleTimerReset();
}
catch (Exception e)
{
// TODO
}
}
------解决方案--------------------
使用API函数SetThreadExecutionState,
要阻止系统自动待机:
SetThreadExecutionState(ES_SYSTEM_REQUIRED | ES_DISPLAY_REQUIRED | ES_CONTINUOUS);
取消自动待机:
SetThreadExecutionState(ES_CONTINUOUS);
如果要组织用户手动让电脑待机,需要处理WM_POWERBROADCAST消息,在消息响应函数中返回TRUE,即可阻止系统待机。
RT
查了网上的资料,说可以定时的使用SystemIdleTimerReset();但是怎么样获取这个时间频率呢,或者说如何获取系统从无输入情况下到进入待机时间。。或者有什么更好的方法呢。。高手答疑下。。。
------解决方案--------------------
我觉得启动个Timer,每30秒调用SystemIdleTimerReset一次,对系统和程序性能影响都不大。
------解决方案--------------------
查过注册表,里面有时间!
就是系统设置中的值在注册表里面有。
------解决方案--------------------
/// <summary>
/// This function resets a system timer that controls whether or not the
/// device will automatically go into a suspended state.
/// </summary>
[DllImport("CoreDll.dll")]
public static extern void SystemIdleTimerReset();
private static int nDisableSleepCalls = 0;
private static System.Threading.Timer preventSleepTimer = null;
public static void DisableDeviceSleep()
{
nDisableSleepCalls++;
if (nDisableSleepCalls == 1)
{
Debug.Assert(preventSleepTimer == null);
// start a 30-second periodic timer
preventSleepTimer = new System.Threading.Timer(new TimerCallback(PokeDeviceToKeepAwake),
null, 0, 30 * 1000);
}
}
public static void EnableDeviceSleep()
{
nDisableSleepCalls--;
if (nDisableSleepCalls == 0)
{
Debug.Assert(preventSleepTimer != null);
if (preventSleepTimer != null)
{
preventSleepTimer.Dispose();
preventSleepTimer = null;
}
}
}
private static void PokeDeviceToKeepAwake(object extra)
{
try
{
PlatformUtil.SystemIdleTimerReset();
}
catch (Exception e)
{
// TODO
}
}
------解决方案--------------------
使用API函数SetThreadExecutionState,
要阻止系统自动待机:
SetThreadExecutionState(ES_SYSTEM_REQUIRED | ES_DISPLAY_REQUIRED | ES_CONTINUOUS);
取消自动待机:
SetThreadExecutionState(ES_CONTINUOUS);
如果要组织用户手动让电脑待机,需要处理WM_POWERBROADCAST消息,在消息响应函数中返回TRUE,即可阻止系统待机。