Date: 02-16-2022
Return to Index
created by gbSnippets
'Credit: Peter Lameijn
http://www.powerbasic.com/support/pbforums/showthread.php?t=21077&highlight=GetPRocesstimes
'------------------------------------------------------------------------------
' Get CPU usage in % (in this case of notepad)
' Updated every 500 mS
'------------------------------------------------------------------------------
#Compile Exe
#Dim All
#Include "Win32api.inc"
Global hProcess As Dword, hShell As Dword, hDlg As Long
%LBL = %WM_USER + 1024
Type QuadFileTime
FTime As Quad
End Type
'------------------------------------------------------------------------------
Function GetProcessUsage(hHandle As Dword) Export As Dword
Local Ret As Long, ValKFTDiff As Quad, ValUFTDiff As Quad
Static CFT As QuadFileTime, EFT As QuadFileTime, KFT As QuadFileTime
Static OldKFT As QuadFileTime, OldUFT As QuadFileTime, UFT As QuadFileTime
Static ValKFT As Quad, OldValKFT As Quad, ValUFT As Quad, OldValUFT As Quad
Ret = GetProcessTimes (hHandle, CFT, EFT, KFT, UFT)
If Ret = 0 Then MsgBox "GetProcessTimes failed!",,"Error:"
OldValKFT = ValKFT
OldValUFT = ValUFT
ValKFT = KFT.FTime
ValUFT = UFT.FTime
If OldValKFT < ValKFT Then
ValKFTDiff = ValKFT - OldValKFT
Else
ValKFTDiff = (&hffff-OldValKFT) + ValKFT
End If
If OldValUFT < ValUFT Then
ValUFTDiff = ValUFT - OldValUFT
Else
ValUFTDiff = (&hffff-OldValUFT) + ValUFT
End If
Ret = (ValUFTDiff + ValKFTDiff) \ 50000
If Ret < 0 Then Ret = 0 : If Ret > 100 Then Ret = 100
Function = Ret
End Function
'------------------------------------------------------------------------------
CallBack Function CbMain()
Local Ret As Long
Select Case CbMsg
Case %WM_TIMER
Ret = GetProcessUsage (hProcess)
Control Set Text hDlg, %LBL, "CPU Load (%):" & Str$(Ret)
End Select
End Function
'------------------------------------------------------------------------------
Function PbMain () As Long
Local Ret As Long
hShell = Shell ("notepad")
hProcess = OpenProcess(ByVal %PROCESS_ALL_ACCESS,ByVal 0,hShell)
If hProcess = 0 Then MsgBox "OpenProcess failed!",,"Error:"
Dialog New 0, "CPU Usage",,,80,50, %WS_SYSMENU, %WS_EX_TOPMOST To hDlg
SetTimer hDlg, &hFFFF,500,ByVal %Null
Control Add Label, hDlg, %LBL,"CPU Load (%):", 5, 15, 70, 12
Dialog Show Modal hDlg Call CbMain
End Function
'------------------------------------------------------------------------------
------------------
'gbs_01370
'Date: 05-11-2013
http://www.garybeene.com/sw/gbsnippets.htm