Have a look at the unmanaged API functions GetSystemPowerStatus
and SetThreadExecutionState
. Using a (thread) timer, you can periodically update the status, e.g. from a class property, and inform the system about your requirements. This is useful, if your application may allow or disallow the screensaver, depending on it's operating state.
public class PowerManager : IDisposable
{
[Flags]
public enum ExecutionStateEnum : uint
{
LetTheSystemDecide = 0x00,
SystemRequired = 0x01,
SystemDisplayRequired = 0x02,
UserPresent = 0x04,
Continuous = 0x80000000,
}
[DllImport("kernel32")]
private static extern uint SetThreadExecutionState(ExecutionStateEnum esFlags);
public PowerManager() {}
public Update(ExecutionStateEnum state)
{
SetThreadExecutionState(state);
}
}
Update:
Then call PowerManager.Update(ExecutionStateEnum.SystemDisplayRequired)
to disable the screensaver or call PowerManager.Update(ExecutionStateEnum.LetTheSystemDecide)
to restore the default system behaviour (allow the screensaver).
If the method is called periodically from a timer callback, adjust the timer interval according to the configured screensaver timeout.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…