Hi,
I want to know the general CPU usage, no matter if its dual core, etc. Just the general CPU usage.
Can anybody point me in the right direction?
Thanks in advance
*I know how to do it with RunProgram("cmd.exe","/C wmic cpu get loadpercentage",""), Im looking a more direct way
Get general CPU usage?
Get general CPU usage?
ARGENTINA WORLD CHAMPION
Re: Get general CPU usage?
Code snippet by luis, works both on x32 and x64
Code: Select all
Prototype GetSystemTimes(*lpIdleTime, *lpKernelTime, *lpUserTime )
Procedure.s CPULoad()
Dll = OpenLibrary(#PB_Any,"kernel32.dll")
Global GetSystemTimes.GetSystemTimes = GetFunction(Dll, "GetSystemTimes")
Static FirstRun = #True
Static Dim StartTimes(1), Dim EndTimes(1)
Static CPU_Kernel.FILETIME, CPU_User.FILETIME, CPU_Idle.FILETIME
GetSystemTimes(@CPU_Idle, @CPU_Kernel, @CPU_User)
EndTimes(0) = (CPU_Kernel\dwLowDateTime + CPU_User\dwLowDateTime)
EndTimes(1) = CPU_Idle\dwLowDateTime
If FirstRun
StartTimes(1) = EndTimes(1)
StartTimes(0) = EndTimes(0)
FirstRun = #False
CloseLibrary(Dll)
ProcedureReturn ""
EndIf
Idle_Time = EndTimes(1) - StartTimes(1)
If Idle_Time > 0
Total_Time = EndTimes(0) - StartTimes(0)
CPU_Percent.f = ((Total_Time - Idle_Time) / Total_Time) * 100
Text.s = Left(StrF(CPU_Percent), FindString(StrF(CPU_Percent), ".", 1) + 1) + " %"
EndIf
StartTimes(1) = EndTimes(1)
StartTimes(0) = EndTimes(0)
CloseLibrary(Dll)
ProcedureReturn Text
EndProcedure
; usage example
CPULoad() ; init
Repeat
Delay(1000)
Debug CPULoad()
ForEver
Last edited by ostapas on Mon Aug 24, 2015 7:18 pm, edited 1 time in total.
Re: Get general CPU usage?
@Ostapas
its not working for me or maqybe i am missing something.
---------------------------------------------
Using the RunProgram("cmd.exe","/C wmic cpu get loadpercentage","") make the CPU load increases, so the number i get its not the CPU load that was before running it.
its not working for me or maqybe i am missing something.
---------------------------------------------
Using the RunProgram("cmd.exe","/C wmic cpu get loadpercentage","") make the CPU load increases, so the number i get its not the CPU load that was before running it.
ARGENTINA WORLD CHAMPION
Re: Get general CPU usage?
Hi,
use:
The first call is only a dummy call to initialize the start values.
Bernd
use:
Code: Select all
Debug CPULoad()
Delay(100)
Debug CPULoad()
Delay(100)
Debug CPULoad()
Bernd
Re: Get general CPU usage?
Yes, i notice that, byt was not using delay, my bad.infratec wrote:Hi,
The first call is only a dummy call to initialize the start values.
Bernd
Thanks

ARGENTINA WORLD CHAMPION
Re: Get general CPU usage?
Thanks for sharing ostapas.
➽ Windows 11 64-bit - PB 6.21 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti
Sorry for my bad english and the Dunning–Kruger effect
Re: Get general CPU usage?
I would replace OpenLibrary with
and remove the CloseLibrary calls
Code: Select all
Static DLL
If Not IsLibrary(DLL)
Dll = OpenLibrary(#PB_Any,"kernel32.dll")
EndIf
Programming on Windows 10 Pro / Windows 7 Enterprise / Linux Mint 19 / Manjaro Linux with PB 5, Python 3 and Go
Re: Get general CPU usage?
Nah, just do this:
Code: Select all
EnableExplicit
Import "kernel32.lib"
GetSystemTimes(*lpIdleTime, *lpKernelTime, *lpUserTime)
EndImport
Procedure.s CPULoad()
Static FirstRun = #True
Static Dim StartTimes(1), Dim EndTimes(1)
Static CPU_Kernel.FILETIME, CPU_User.FILETIME, CPU_Idle.FILETIME
Protected Idle_Time.i
Protected Total_Time.i
Protected CPU_Percent.f
Protected Text.s
GetSystemTimes(@CPU_Idle, @CPU_Kernel, @CPU_User)
EndTimes(0) = (CPU_Kernel\dwLowDateTime + CPU_User\dwLowDateTime)
EndTimes(1) = CPU_Idle\dwLowDateTime
If FirstRun
StartTimes(1) = EndTimes(1)
StartTimes(0) = EndTimes(0)
FirstRun = #False
ProcedureReturn ""
EndIf
Idle_Time = EndTimes(1) - StartTimes(1)
If Idle_Time > 0
Total_Time = EndTimes(0) - StartTimes(0)
CPU_Percent.f = ((Total_Time - Idle_Time) / Total_Time) * 100
Text.s = Left(StrF(CPU_Percent), FindString(StrF(CPU_Percent), ".", 1) + 1) + " %"
EndIf
StartTimes(1) = EndTimes(1)
StartTimes(0) = EndTimes(0)
ProcedureReturn Text
EndProcedure
; usage example
CPULoad() ; init
Repeat
Delay(1000)
Debug CPULoad()
ForEver
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
Re: Get general CPU usage?
I just wanted to stick with the original code and point out that closing and opening the library all the time might not be the best way.
However, importing the lib file directly sure is a good way to go
However, importing the lib file directly sure is a good way to go

Programming on Windows 10 Pro / Windows 7 Enterprise / Linux Mint 19 / Manjaro Linux with PB 5, Python 3 and Go