VBcoders Browse New Submit Contact Sign In

No account? Register free

Forgot password?

CPU Utilization

James Vincent Carnicelli  (21 Submissions)   Windows System Services   Visual Basic 5.0   Intermediate   Wed 3rd February 2021

I created this because I needed to be able to adjust how many child processes my batch processing app created so that it didn't I created this demo because I needed to be able to adjust how many child processes my batch processing app created so that it didn't overutilize the CPU.
To duplicate this, create a form with one text box (Text1) and one timer (Timer1) in the configuration you see in the screen shot. Then paste the code seen here.
The main key is the windows idle process (PID = 0), which eats up whatever CPU cycles are not used by other apps. Every so often, this code takes a sample of how much CPU time, in seconds, that process has taken up since it started. Then it takes the delta to calculate its own CPU utilization. Subtract it from 100% and you get the CPU utlization.
In this case, we're using the Windows Management Interface (WMI) to get this information -- mainly because the API calls to get process times won't work for the idle process for security reasons. This should work for Windows 2000 and XP, but probably not for NT 4.0- or Windows 3.x, 9x, or ME.
You can morph this sample a lot of ways. For example, increase the SampleRate variable to get a smoother variation curve over time or decrease to get more immediate values. You can use additional parameters in Locator.ConnectServer() to connect to a remote machine, as another example. See here for more about the WMI objects:
https://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/wmi_reference.asp
You
could also easily modify this to track some other process' CPU utilization by its PID. Or you could also use it to track memory or other resource utilization.
- Cheers,
Jim Carnicelli

Rate CPU Utilization (4(4 Vote))
CPU Utilization.bas

CPU Utilization Comments

No comments yet — be the first to post one!

Post a Comment

0/1000 characters