Code: Select all
;###################################################################################################################
Global gCurrentUsage.d
Procedure MonitorCPU(void)
creation.FILETIME
exit.FILETIME
kernel.FILETIME
user.FILETIME
sysinfo.SYSTEM_INFO
GetSystemInfo_(@sysinfo)
numprocs = sysinfo\dwNumberOfProcessors
h = OpenProcess_(#PROCESS_QUERY_INFORMATION, #False, GetCurrentProcessId_())
GetProcessTimes_(h,@creation,@exit,@kernel,@user)
oldkernel=kernel\dwlowdatetime
olduser=user\dwlowdatetime
Delay(500)
Repeat
GetProcessTimes_(h,@creation,@exit,@kernel,@user)
gCurrentUsage.d = ((((user\dwlowdatetime-olduser)+(kernel\dwlowdatetime-oldkernel))/500)/100)/numprocs
olduser=user\dwlowdatetime
oldkernel=kernel\dwlowdatetime
Delay (500)
ForEver
EndProcedure
;###################################################################################################################
; Little test program - shows around 3% here, same as task mgr and debugger's CPU monitor
CreateThread(@MonitorCPU(),0)
InitSprite()
OpenWindow(0,0,0,640,480,"CPU Load Test",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(0),0,0,640,480,0,0,0)
CreateSprite(0, 128,128, #PB_Sprite_Texture)
StartDrawing(SpriteOutput(0))
Box(0,0,128,128,#Red)
StopDrawing()
InitSprite3D()
CreateSprite3D(0,0)
CreateSprite3D(1,0)
CreateSprite3D(2,0)
Repeat
ClearScreen(0)
StartDrawing(ScreenOutput())
DrawText(0,0,"CPU Usage: "+StrD(gCurrentUsage,2), #White, #Black)
StopDrawing()
Start3D()
RotateSprite3D(0,1,1)
RotateSprite3D(1,1,1)
RotateSprite3D(2,1,1)
DisplaySprite3D(0,100,100)
DisplaySprite3D(1,250,100)
DisplaySprite3D(2,400,100)
Stop3D()
FlipBuffers()
Delay(1)
ev = WindowEvent()
Until ev = #WM_CLOSE
This would actually be useful to make into a library and call it in your WIP projects to keep an eye on your CPU load, then you could take it out at release. I might make a little sticky window with a dial or something, could be fun.