In this tutorial, I will teach you how to create a CPU and RAM meter in VB.NET. With this, you will be able to determine the usage and it calculates the percentage of the RAM and CPU in your computer.
Open Visual Basic, create a new Windows Form Application and drag the Label, ProgressBar, PerformanceCounter and a Timer. Name the two PerformanceCounters, “pc_CPU” and the other one is “pc_RAM”. Name the two Labels into “lbl_CPU” and “lbl_RAM”. Then, name the two ProgressBars into “pb_CPU”and “pb_RAM”.
After setting up the Form, click the “pc_CPU” PerformanceCounter and go to the properties. In properties of “pc_CPU”, select “Processor” for the Category Name,“% Processor Time” for the Counter Name and “_Total” for the Instance Name.
After setting up the properties of the “pc_CPU”, click the other PerformanceCounter named “pc_RAM” and go to the properties. In the properties, select “Memory” for Category Name and “ % Commited Bytes in Use” for the Counter Name.
Now, double click a Timer to fire the “Tick” event handler. In the “Tick” event handler, you have to setup the performance and the percentage of the CPU and RAM of the computer.
- Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
- 'SET THE PERFORMANCE VALUE OF THE RAM AND CPU TO THE PROGESSBAR
- pb_CPU.Value = pc_CPU.NextValue
- pb_RAM.Value = pc_RAM.NextValue
- 'SET THE PROGRESS BAR VALUE TO THE LABEL TO KNOW WHAT IS THE PERCENTAGE OF THE PROCESS.
- lbl_cpu.Text = pb_CPU.Value & "%"
- lbl_ram.Text = pb_RAM.Value & "%"
- End Sub
Go back to the Design Views and double click the Form. In the
Form1_Load, you have to start the timer and setup the interval of it.
- Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- 'INITIALIZE THE INTERVAL OF THE TIMER TO 500.
- Timer1.Interval = 500
- 'START THE TIMER
- End Sub
You can download the Complete Source Code.