Waiting for CPU usage?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Francois84
Posts: 6
Joined: 25 Jun 2022, 02:46

Waiting for CPU usage?

Post by Francois84 » 25 Jun 2022, 02:53

Hi,

is it possible to create a function that sleep/wait until CPU usage is under 10%?


User avatar
mikeyww
Posts: 26589
Joined: 09 Sep 2014, 18:38

Re: Waiting for CPU usage?

Post by mikeyww » 25 Jun 2022, 05:45

Code: Select all

#SingleInstance Force
Process, Priority,, B
While ((9 < cpu := CPULoad()) || !cpu) {
 ToolTip, %cpu%
 Sleep, 1000
}
ToolTip
MsgBox, 64, CPU load, %cpu%`%

CPULoad() { ; CPULoad() by SKAN
 ; https://github.com/jNizM/SysMeter/blob/master/src/SysMeter.ahk#L335
 Static PIT, PKT, PUT
 If (PIT = "")
  Return 0, DllCall("GetSystemTimes", "Int64P", PIT, "Int64P", PKT, "Int64P", PUT)
 DllCall("GetSystemTimes", "Int64P", CIT, "Int64P", CKT, "Int64P", CUT)
 IdleTime := PIT - CIT, KernelTime := PKT - CKT, UserTime := PUT - CUT
 SystemTime := KernelTime + UserTime
 Return (SystemTime - IdleTime) * 100 // SystemTime, PIT := CIT, PKT := CKT, PUT := CUT
}

Francois84
Posts: 6
Joined: 25 Jun 2022, 02:46

Re: Waiting for CPU usage?

Post by Francois84 » 25 Jun 2022, 07:01

mikeyww wrote:
25 Jun 2022, 05:45

Code: Select all

#SingleInstance Force
Process, Priority,, B
While ((9 < cpu := CPULoad()) || !cpu) {
 ToolTip, %cpu%
 Sleep, 1000
}
ToolTip
MsgBox, 64, CPU load, %cpu%`%

CPULoad() { ; CPULoad() by SKAN
 ; https://github.com/jNizM/SysMeter/blob/master/src/SysMeter.ahk#L335
 Static PIT, PKT, PUT
 If (PIT = "")
  Return 0, DllCall("GetSystemTimes", "Int64P", PIT, "Int64P", PKT, "Int64P", PUT)
 DllCall("GetSystemTimes", "Int64P", CIT, "Int64P", CKT, "Int64P", CUT)
 IdleTime := PIT - CIT, KernelTime := PKT - CKT, UserTime := PUT - CUT
 SystemTime := KernelTime + UserTime
 Return (SystemTime - IdleTime) * 100 // SystemTime, PIT := CIT, PKT := CKT, PUT := CUT
}
Thx a lot, i will give this a try :)

Post Reply

Return to “Ask for Help (v1)”