Code : Tout sélectionner
Enumeration Font
#FontSprite
EndEnumeration
Enumeration Window
#mainform
EndEnumeration
Enumeration Sprite
#CpuUsage
EndEnumeration
Declare.d GetProcessCpuLoad (pid)
Declare RandomSign()
Define.i PID = GetCurrentProcessId_() ;Identification de notre application
Define.d CpuUsage, CpuUsageMin, CpuUsageMax
Define.i Radius
Structure NewSprite
id.i
x.i
y.i
sens.i
EndStructure
NewList Sprites.NewSprite()
;Initialisation
InitSprite() : InitKeyboard() : InitMouse()
;Font
LoadFont(#FontSprite, "Courrier", 15)
;Ouverture de la fenetre de l'application
OpenWindow(#mainform, 0, 0, 800, 600, "Test charge CPU", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
;Fenetre du rendu de la scene
OpenWindowedScreen(WindowID(#mainform), 0, 0, 800, 600)
;Un sprite qui affichera la charge CPU
CreateSprite(#CpuUsage, 800, 35, #PB_Sprite_AlphaBlending)
;Boucle evenementielle
Repeat ;Evenement window
CpuUsage = GetProcessCpuLoad(PID)
If CpuUsage > CpuUsageMax
CpuUsageMax = CpuUsage
EndIf
If CpuUsage < CpuUsageMin Or CpuUsageMin = 0
CpuUsageMin = CpuUsage
EndIf
Repeat;Evenement screen
Event = WindowEvent()
Select Event
Case #PB_Event_CloseWindow
End
EndSelect
Until Event=0
;Mise à jour de la charge Cpu dans un sprite
StartDrawing(SpriteOutput(#CpuUsage))
DrawingMode(#PB_2DDrawing_AllChannels)
Box(0, 0, 800, 35, RGBA(0, 0, 0, 128))
DrawingMode(#PB_2DDrawing_Transparent)
DrawingFont(FontID(#FontSprite))
DrawText(5, 5, "Charge CPU " + StrD(CpuUsage, 2) + "%")
DrawText(300, 5, "Min " + StrD(CpuUsageMin, 2) + "%")
DrawText(450, 5, "Max " + StrD(CpuUsageMax, 2) + "%")
DrawText(650, 5, "Sprites " + Str(ListSize(Sprites())))
StopDrawing()
;Affichage de la charge CPU (Header)
DisplayTransparentSprite(#CpuUsage, 0, 0)
;On va faire travailler un peu le CPU
If ListSize(Sprites()) <> 2000
AddElement(Sprites())
With Sprites()
\id = CreateSprite(#PB_Any, 100, 100, #PB_Sprite_AlphaBlending)
\x = Random(700, 0)
\y = Random(500, 100)
\sens = RandomSign()
StartDrawing(SpriteOutput(\id))
DrawingMode(8)
Box(0,0,100,100,RGBA(0,0,0,0))
DrawingMode(16)
Radius = Random(15, 5)
Circle(50,50, Radius * 2,RGBA(255,255,255,80))
Circle(50,50, Radius,RGBA(255,255,255,255))
StopDrawing()
EndWith
EndIf
ForEach Sprites()
With Sprites()
\x + \sens
\y + \sens
If \x < 0 Or \x > 700 Or \y < 100 Or \y > 500
\sens * -1
EndIf
DisplayTransparentSprite(\id, \x, \y)
EndWith
Next
FlipBuffers()
ClearScreen(RGB(135, 206, 235))
ExamineKeyboard()
;Delay ou pas delay
;Delay(2)
Until KeyboardPushed(#PB_Key_Escape)
Procedure.d GetProcessCpuLoad (pid)
Static LastProcessTime.l = 0
Static LastSystemTime.l = 0
Static dCpuUsage.d
Protected iCores = CountCPUs(#PB_System_CPUs)
Protected hProcess = OpenProcess_(#PROCESS_QUERY_INFORMATION,#False,pid)
If (hProcess)
Protected CurrentProcessTime.l, CurrentSystemTime.l
Protected ftCreationTime.FILETIME , ftExitTime.FILETIME , ftKernelTime.FILETIME , ftUserTime.FILETIME
Protected KernelTime.ULARGE_INTEGER , UserTime.ULARGE_INTEGER
If GetProcessTimes_(hProcess, @ftCreationTime, @ftExitTime, @ftKernelTime, @ftUserTime)
KernelTime\HighPart = ftKernelTime\dwHighDateTime
KernelTime\LowPart = ftKernelTime\dwLowDateTime
UserTime\HighPart = ftUserTime\dwHighDateTime
UserTime\LowPart = ftUserTime\dwLowDateTime
CurrentProcessTime = (PeekQ(@KernelTime) + PeekQ(@UserTime)) / (iCores * 100)
CurrentSystemTime = GetTickCount_() ; use GetTickCount64() if needed
If(LastSystemTime)
dCpuUsage = (CurrentProcessTime - LastProcessTime + 0.0) / (CurrentSystemTime - LastSystemTime)
If CurrentSystemTime - LastSystemTime > 3000 ; smoothing
LastSystemTime = 0
LastProcessTime = 0
EndIf
Else
LastProcessTime = CurrentProcessTime
LastSystemTime = CurrentSystemTime
EndIf
EndIf
CloseHandle_(hProcess)
ProcedureReturn dCpuUsage
EndIf
ProcedureReturn -1 ; wrong PID
EndProcedure
Procedure RandomSign()
ProcedureReturn Random(1)*2-1
EndProcedure