Page 1 of 1
How can I keep my computer awake?
Posted: Tue Dec 22, 2015 2:48 am
by ozzie
I know that under Windows Power Options I can select or set a plan that has 'put the computer to sleep' to 'never', but I also know that many of my users don't do that, even though I recommend it for live productions. So is there a way in my program that (a) I can check what the current setting is for 'put the computer to sleep', and/or (b) do something periodically in my program that will force the computer to stay awake?
What's happening at the moment is that if the computer goes to sleep whilst playing an audio file via BASS ASIO then playback stops. On debugging this problem we have come to the conclusion that it is probably a driver issue, but even so we need to somehow keep the computer awake or at least warn the user that they haven't selected the correct power plan.
Re: How can I keep my computer awake?
Posted: Tue Dec 22, 2015 3:25 am
by TassyJim
The Windows command line program PowerCfg.exe might be a good place to start.
PowerCfg.exe /? for help
and
PowerCfg.exe /Q
to give you pages of information to wade through.
Jim
Re: How can I keep my computer awake?
Posted: Tue Dec 22, 2015 3:48 am
by ar-s
If you want to use PB, You can make a small app witch move the mouse each N minutes.
Re: How can I keep my computer awake?
Posted: Tue Dec 22, 2015 4:25 am
by Lunasole
There is win32 api to get recent user activity time:
Code: Select all
Define f.LASTINPUTINFO
f\cbSize = SizeOf(f)
GetLastInputInfo_(f)
Debug f\dwTime
I guess windows goes to sleep depending on this data. So you can try monitor this value and emulate kind of invisible user action if it reaches some time, (for example, moving mouse by 1 pixel right and back, or emulate scroll lock pressed twice, etc) to update last activity time (but it also will reset the screensaver timeout and maybe something else). Didn't tried if it is all as I said, just theory.
Re: How can I keep my computer awake?
Posted: Tue Dec 22, 2015 4:50 am
by heartbone
Look into resetting the sleep timer, intuitively it seems like it should be something there.
Re: How can I keep my computer awake?
Posted: Tue Dec 22, 2015 8:44 am
by Shardik
You may take a look into
this code example from Fluid Byte which demonstrates how to detect a system's entering and resuming of standby mode. It's utilizing Windows'
WM_POWERBROADCAST message.
To prevent a system from transitioning to a low-power state, you should take a look into
SetThreadExecutionState.
Re: How can I keep my computer awake?
Posted: Tue Dec 22, 2015 12:31 pm
by ozzie
Many thanks for the various suggestions. Shardik's suggestion to use
SetThreadExecutionState seems to have done the trick. I've just run another test using this command, this time leaving the program playing a looping cue for about 112 minutes, and it was still playing. On previous tests on this machine it would have stopped after 30 minutes.
I called this command near the start of the program:
Code: Select all
SetThreadExecutionState(#ES_AWAYMODE_REQUIRED | #ES_SYSTEM_REQUIRED | #ES_CONTINUOUS)
I also had to set up a prototype etc for SetThreadExecutionState as PB doesn't seem to know about SetThreadExecutionState_, although it does know about the #ES... constants.
Re: How can I keep my computer awake?
Posted: Tue Dec 22, 2015 12:59 pm
by Dude
Can you post the Prototype info, please? I need to learn how they work.

Re: How can I keep my computer awake?
Posted: Tue Dec 22, 2015 1:14 pm
by nco2k
@ozzie
or you could simply import the function:
Code: Select all
CompilerIf Defined(SetThreadExecutionState_, #PB_OSFunction) = #False
Import "Kernel32.lib"
CompilerIf #PB_Processor_x86
SetThreadExecutionState_(esFlags.l) As "_SetThreadExecutionState@4"
CompilerElse
SetThreadExecutionState_(esFlags.l) As "SetThreadExecutionState"
CompilerEndIf
EndImport
CompilerEndIf
c ya,
nco2k
Re: How can I keep my computer awake?
Posted: Tue Dec 22, 2015 4:09 pm
by bbanelli
Maybe this will help?
https://msdn.microsoft.com/en-us/librar ... s.85).aspx
Code: Select all
EnableExplicit
#POWER_REQUEST_CONTEXT_VERSION = 0
#POWER_REQUEST_CONTEXT_SIMPLE_STRING = 1
#POWER_REQUEST_CONTEXT_DETAILED_STRING = 2
Enumeration PowerRequestType
#PowerRequestDisplayRequired
#PowerRequestSystemRequired
#PowerRequestAwayModeRequired
CompilerIf #PB_Compiler_OS > #PB_OS_Windows_7 Or #PB_Compiler_OS > #PB_OS_Windows_Server_2008_R2
#PowerRequestExecutionRequired
CompilerEndIf
EndEnumeration
Structure DetailedStructure
LocalizedReasonModule.l
LocalizedReasonId.l
ReasonStringCount.l
*ReasonStrings
EndStructure
Structure REASON_CONTEXT_Structure
Version.l
Flags.l
StructureUnion
*Detailed.DetailedStructure
*SimpleReasonString
EndStructureUnion
EndStructure
Prototype.i Prototype_PowerCreateRequest(*REASON_CONTEXT.REASON_CONTEXT_Structure)
Prototype.i Prototype_PowerSetRequest(PowerRequest.i, RequestType.i)
Prototype.i Prototype_PowerClearRequest(PowerRequest.i, RequestType.i)
Global PowerCreateRequest.Prototype_PowerCreateRequest
Global PowerSetRequest.Prototype_PowerSetRequest
Global PowerClearRequest.Prototype_PowerClearRequest
Define.i hndl, lib
Define ReasonContext.REASON_CONTEXT_Structure
lib = OpenLibrary(#PB_Any, "Kernel32.dll")
If lib
PowerCreateRequest = GetFunction(lib, "PowerCreateRequest")
PowerSetRequest = GetFunction(lib, "PowerSetRequest")
PowerClearRequest = GetFunction(lib, "PowerClearRequest")
CloseLibrary(lib)
ReasonContext\Version = #POWER_REQUEST_CONTEXT_VERSION
ReasonContext\Flags = #POWER_REQUEST_CONTEXT_SIMPLE_STRING
ReasonContext\SimpleReasonString = @"Your Custom Reason To Prevent Standby"
hndl = PowerCreateRequest(@ReasonContext)
If hndl <> #INVALID_HANDLE_VALUE
If PowerSetRequest(hndl, #PowerRequestSystemRequired)
Delay(20000)
PowerClearRequest(hndl, #PowerRequestSystemRequired)
EndIf
CloseHandle_(hndl)
EndIf
EndIf
Re: How can I keep my computer awake?
Posted: Tue Dec 22, 2015 11:43 pm
by ozzie
Thanks, nc02k - using Import is much easier and safer than creating and using a Prototype.
btw, I've now added #ES_DISPLAY_REQUIRED to the flags, to keep the display alive. So here's the latest code (after adding the Import coding):
Code: Select all
SetThreadExecutionState_(#ES_AWAYMODE_REQUIRED | #ES_SYSTEM_REQUIRED | #ES_CONTINUOUS | #ES_DISPLAY_REQUIRED)