How to Create CPU and RAM Monitor with Real time Charts in C#
By FoxLearn 7/16/2024 8:30:02 AM 22.33K
We will create a CPU and RAM monitor with real-time charts in C# using System.Diagnostics
library for accessing CPU and RAM usage information, and using Chart control for real-time charting.
How to Create CPU and RAM Monitor with Real time Charts in C#
Ram and CPU Meter is a simple application in c# that helps you know your computer's processor and memory usage.
The PerformanceCounter component provides information as to how well the operating system or an application, service or driver is performing. You should use the Performance Counters API when you want to provide or consume counter data.
Below is a simple example demonstrating how to create such a monitor.
Drag the MetroLabel, MetroProgressBar and Chart controls from your Visual Studio toolbox to your winform, then you can layout your UI as shown below.
To create a metro form using MetroFramework you should change inherit from your Form to MetroForm.
If you don't see the metro framework in your Visual Toolbox, you can view How to download and install metro framework
Add the Timer and PerformanceCounter controls to your metro form, then add a tick event handler to the timer control allows you to update your real time chart as the following c# code.
private void timer_Tick(object sender, EventArgs e) { float fcpu = pCPU.NextValue(); float fram = pRAM.NextValue(); //Set value to cpu and ram metroProgressBarCPU.Value = (int)fcpu; metroProgressBarRAM.Value = (int)fram; //Update value to cpu and ram label lblCPU.Text = string.Format("{0:0.00}%", fcpu); lblRAM.Text = string.Format("{0:0.00}%", fram); //Draw cpu and ram chart chart1.Series["CPU"].Points.AddY(fcpu); chart1.Series["RAM"].Points.AddY(fram); }
You'll also need a Timer
control named timer
,then use PerformanceCounter
objects for monitoring CPU and RAM usage. It will update the charts with the current CPU and RAM usage every tick of the timer.
And don't forget to call the Start method of the timer in your Form_Load event as shown below.
private void Form1_Load(object sender, EventArgs e) { timer.Start(); }
VIDEO TUTORIAL