How can I keep my computer awake?

Windows specific forum
ozzie
Enthusiast
Enthusiast
Posts: 443
Joined: Sun Apr 06, 2008 12:54 pm
Location: Brisbane, Qld, Australia
Contact:

How can I keep my computer awake?

Post 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.
TassyJim
Enthusiast
Enthusiast
Posts: 191
Joined: Sun Jun 16, 2013 6:27 am
Location: Tasmania (Australia)

Re: How can I keep my computer awake?

Post 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
User avatar
ar-s
Enthusiast
Enthusiast
Posts: 344
Joined: Sat Oct 06, 2007 11:20 pm
Location: France

Re: How can I keep my computer awake?

Post by ar-s »

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

Code: Select all

r3p347 : 7ry : un71l d0n3 = 1
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

Re: How can I keep my computer awake?

Post 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.
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
User avatar
heartbone
Addict
Addict
Posts: 1058
Joined: Fri Apr 12, 2013 1:55 pm
Location: just outside of Ferguson

Re: How can I keep my computer awake?

Post by heartbone »

Look into resetting the sleep timer, intuitively it seems like it should be something there.
Keep it BASIC.
User avatar
Shardik
Addict
Addict
Posts: 2069
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: How can I keep my computer awake?

Post 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.
ozzie
Enthusiast
Enthusiast
Posts: 443
Joined: Sun Apr 06, 2008 12:54 pm
Location: Brisbane, Qld, Australia
Contact:

Re: How can I keep my computer awake?

Post 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.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: How can I keep my computer awake?

Post by Dude »

Can you post the Prototype info, please? I need to learn how they work. :)
User avatar
nco2k
Addict
Addict
Posts: 1344
Joined: Mon Sep 15, 2003 5:55 am

Re: How can I keep my computer awake?

Post 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
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
User avatar
bbanelli
Enthusiast
Enthusiast
Posts: 544
Joined: Tue May 28, 2013 10:51 pm
Location: Europe
Contact:

Re: How can I keep my computer awake?

Post 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
"If you lie to the compiler, it will get its revenge."
Henry Spencer
https://www.pci-z.com/
ozzie
Enthusiast
Enthusiast
Posts: 443
Joined: Sun Apr 06, 2008 12:54 pm
Location: Brisbane, Qld, Australia
Contact:

Re: How can I keep my computer awake?

Post 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)
Post Reply