Standby Prevention at CPU load

Post your working scripts, libraries and tools for AHK v1.1 and older
SvenBent
Posts: 266
Joined: 09 Aug 2015, 01:34

Standby Prevention at CPU load

13 Mar 2016, 15:09

I've seen a lot of AHK script out on the internet to prevent the PC to go standby if the CPU load is high. But they all have one "flaw" in my eyes, that they all depend on moving mouse and or push a key wish wil; prevent BOTH monitor and system to go standby
Instead i offer this little solution which only prevents system standby but allows your monitor to go into a standby states. This is perfect for PPL that use their PC for large number crunching tasks like 3D rendering, video encoding or data compression.


Code: Select all

Loop
Loop
	{
	Sleep 30000				;Sleep timer to avoud high cpu usage. 30s ensures 2 checks per min, so even 1 min standby time works
	if A_TimeIdle > 20000			; verify if machine has been "idle" more than 20 sek. 
		{
		CPULoad()			;init of CPULoad()  check
		PwrPrevState:=0			;init of Power prevent states variabel
		While A_TimeIdle > 20000	
			{
			Sleep 9999		; Sleep timer to both reduce cpu and get time for CPUload() to get a good tome to average over
			Check:=CPULoad()
			if (Check > 8)		; Checks if CPU load is above 8%
				{
				Gosub, PrevStandby	; Calls the prevent standby routine. since CPU load is high
				}
			else
				{
				Gosub, AllowStandby	; calls the enable standby routine since CPU load is low
				}
			}
		}
	Gosub, AllowStandby				; calls the enable standby routine since idletime is low, 
	}						
Return


; The pwrprevstate variable is a only there to save unneeded DLLcalls and thereby decrease overall cpu usage
; if you don't want it, you can remove the init  above and replace the gosub calls with the corresponding DLLcalls's
; and remove the two labels/functions below


CPULoad()
	{
	static PIT, PKT, PUT
	static CIT, CKT, CUT
	static ST
	PIT := CIT
	PKT := CKT
	PUT := CUT
 	DllCall( "GetSystemTimes", "Int64P",CIT, "Int64P",CKT, "Int64P",CUT )
	ST := ((PKT - CKT) + (PUT - CUT))
	Return (( ST - (PIT - CIT)) * 100 ) // ST
 	}

PrevStandby:
if PwrPrevState = 0
	{
	PwrPrevState:=1
	DllCall("Kernel32\SetThreadExecutionState" , "UInt", 0x80000001)	;prevents system but not monitor to go standby
	}
return

AllowStandby:
if PwrPrevState = 1
	{
	PwrPrevState:=0
	DllCall("Kernel32\SetThreadExecutionState" , "UInt", 0x80000000)	; Allows standby again.
	}
return


Sorry for the disorganized code but i hope somebody can form/use it for something.
User avatar
Bon
Posts: 17
Joined: 11 Jan 2014, 07:31

Re: Standby Prevention at CPU load

14 Mar 2016, 06:25

Another approach would be to crank down CPU speed if the temperature rises too high - http://www.autohotkey.com/board/topic/1 ... on-script/
Quidquid Latine dictum sit altum videtur
"Anything said in Latin sounds profound"
SvenBent
Posts: 266
Joined: 09 Aug 2015, 01:34

Re: Standby Prevention at CPU load

17 Mar 2016, 10:04

Nice script.
but i don't get the "another approach" its two very different objective

I wonder if there is an APi call that can set the CPU max P-stepping directly instead of going though making a power profile for it
User avatar
Bon
Posts: 17
Joined: 11 Jan 2014, 07:31

Re: Standby Prevention at CPU load

18 Mar 2016, 11:40

Maybe I misunderstood you - I got the impression that the high CPU load raised the temperature too much, which in turn made the PC go to sleep.
If anyone knows a simpler way to limit the CPU speed I would really like to know about it!
Quidquid Latine dictum sit altum videtur
"Anything said in Latin sounds profound"

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 251 guests