Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Sun Nov 18, 2018 9:29 am
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: [Tut] Get CPUUsage
PostPosted: Mon Apr 23, 2012 5:41 am 
Offline

Joined: Tue Apr 17, 2012 9:26 am
Posts: 5
This isn't probably the best way to do it, but it works on Windows7. (WFS CPUUsage does not)
Sure you could buy custom plugins and do it alot faster, but this way you don't have to pay a fortune for only checking CPUUsage.

First we need to add some objects to the window.
Those are a timer(1000ms) and a label.

Add 3 Properties:
LastIdle as Uint64
LastKernel as Uint64
LastUser as Uint64

Now add a new Module and name it "SystemTimes".
In that Module you will need to add a method with the name "GetIdleTime". (I don't know why I chose that)

Add 3 properties to the module:
lpIdleTime as Uint64
lpKernelTime as Uint64
lpUserTime as Uint64

Now add this code to the GetIdleTime method:
Soft Declare Sub GetSystemTimes Lib "Kernel32" ( byRef IdleTime as Uint64, byRef KernelTime as Uint64, ByRef UserTime as Uint64 )
GetSystemTimes(lpIdleTime, lpKernelTime, lpUserTime)

This code calls the WinAPI to get the system times and store them in the properties we created.

In the Open event (the event that calls when the application opens) you need to add this code:
SystemTimes.GetIdleTime()
LastIdle = SystemTimes.lpIdleTime
LastKernel = SystemTimes.lpKernelTime
LastUser = SystemTimes.lpUserTime

This code calls the method to refresh the values in the module properties, we then add those values to LastIdle, LastKernel and LastUser.


Last you will need to add this code to the Timer:
dim usr, ker, idl, CurrentUser, CurrentKernel, CurrentIdle, cpu, sys as Uint64

SystemTimes.GetIdleTime()
CurrentIdle = SystemTimes.lpIdleTime
CurrentKernel = SystemTimes.lpKernelTime
CurrentUser = SystemTimes.lpUserTime

usr = CurrentUser - LastUser
ker = CurrentKernel - LastKernel
idl = CurrentIdle - LastIdle
sys = ker + usr

cpu = (sys - idl)*100/sys

Label1.text = str(cpu) + "%"

SystemTimes.GetIdleTime()
LastIdle = SystemTimes.lpIdleTime
LastKernel = SystemTimes.lpKernelTime
LastUser = SystemTimes.lpUserTime


First we get the current SystemTimes and use those to calculate the CPUUsage in percentage.
And then print the CPUUsage on the label.
And last we update the last SystemTimes so that they are up to date when we calculate this again in 1000ms.

This code is rough and could probably be polished and better, but I'm a beginner at RealBasic and this is the best I could do.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group