How can I keep my computer awake?
-
ozzie
- Enthusiast

- Posts: 443
- Joined: Sun Apr 06, 2008 12:54 pm
- Location: Brisbane, Qld, Australia
- Contact:
How can I keep my computer awake?
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.
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?
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
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?
If you want to use PB, You can make a small app witch move the mouse each N minutes.
~Ar-S~
My Image Hoster for PB users
My webSite (french) with PB apps : LDVMULTIMEDIA
PB - 3.x / 5.7x / 6 - W11 x64 - Ryzen 7 3700x / #Rpi4
My Image Hoster for PB users
My webSite (french) with PB apps : LDVMULTIMEDIA
PB - 3.x / 5.7x / 6 - W11 x64 - Ryzen 7 3700x / #Rpi4
Code: Select all
r3p347 : 7ry : un71l d0n3 = 1Re: How can I keep my computer awake?
There is win32 api to get recent user activity time:
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.
Code: Select all
Define f.LASTINPUTINFO
f\cbSize = SizeOf(f)
GetLastInputInfo_(f)
Debug f\dwTime"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
Re: How can I keep my computer awake?
Look into resetting the sleep timer, intuitively it seems like it should be something there.
Keep it BASIC.
Re: How can I keep my computer awake?
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.
To prevent a system from transitioning to a low-power state, you should take a look into SetThreadExecutionState.
-
ozzie
- Enthusiast

- Posts: 443
- Joined: Sun Apr 06, 2008 12:54 pm
- Location: Brisbane, Qld, Australia
- Contact:
Re: How can I keep my computer awake?
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:
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.
I called this command near the start of the program:
Code: Select all
SetThreadExecutionState(#ES_AWAYMODE_REQUIRED | #ES_SYSTEM_REQUIRED | #ES_CONTINUOUS)Re: How can I keep my computer awake?
Can you post the Prototype info, please? I need to learn how they work. 
Re: How can I keep my computer awake?
@ozzie
or you could simply import the function:c ya,
nco2k
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
CompilerEndIfnco2k
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
Re: How can I keep my computer awake?
Maybe this will help?
https://msdn.microsoft.com/en-us/librar ... s.85).aspx
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-
ozzie
- Enthusiast

- Posts: 443
- Joined: Sun Apr 06, 2008 12:54 pm
- Location: Brisbane, Qld, Australia
- Contact:
Re: How can I keep my computer awake?
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):
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)