Page 1 of 1
Get general CPU usage?
Posted: Fri Aug 21, 2015 11:02 pm
by ricardo
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
Re: Get general CPU usage?
Posted: Sat Aug 22, 2015 9:01 am
by ostapas
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
Re: Get general CPU usage?
Posted: Mon Aug 24, 2015 1:26 pm
by ricardo
@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.
Re: Get general CPU usage?
Posted: Mon Aug 24, 2015 2:28 pm
by infratec
Hi,
use:
Code: Select all
Debug CPULoad()
Delay(100)
Debug CPULoad()
Delay(100)
Debug CPULoad()
The first call is only a dummy call to initialize the start values.
Bernd
Re: Get general CPU usage?
Posted: Mon Aug 24, 2015 5:01 pm
by ricardo
infratec wrote:Hi,
The first call is only a dummy call to initialize the start values.
Bernd
Yes, i notice that, byt was not using delay, my bad.
Thanks

Re: Get general CPU usage?
Posted: Mon Aug 24, 2015 5:54 pm
by falsam
Thanks for sharing ostapas.
Re: Get general CPU usage?
Posted: Mon Aug 24, 2015 9:26 pm
by Xanos
I would replace OpenLibrary with
Code: Select all
Static DLL
If Not IsLibrary(DLL)
Dll = OpenLibrary(#PB_Any,"kernel32.dll")
EndIf
and remove the CloseLibrary calls
Re: Get general CPU usage?
Posted: Tue Aug 25, 2015 12:44 am
by Shield
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
Re: Get general CPU usage?
Posted: Tue Aug 25, 2015 8:41 am
by Xanos
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
