How to Create CPU and Memory Monitor using Metro Modern UI in C#

By FoxLearn 12/12/2024 2:41:41 PM   8.28K
Creating a CPU and memory monitor in C# with the Metro Framework involves integrating system performance counters and a visually appealing user interface.

In this article, we’ll guide you through creating a Metro-style Windows Forms application using the MetroFramework library. We’ll integrate Timer and PerformanceCounter components to display CPU and RAM usage dynamically.

How to Create CPU and Memory Monitor in C#?

Open Visual Studio, then click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "CPURAM" and then click OK

Right click on your project select Manage NuGet Packages -> Search metro framework -> Install

install metro framework

If you don't see the metro framework in your toolbox, you can view How to download and install metro framework

Drag and drop the MetroLabel, MetroProgressBar controls from Visual Toolbox onto your form designer, then design your metro form as shown below.

cpu meter c#

Add System.Diagnostics for accessing performance counters.

Add PerformanceCounter components to monitor CPU and memory usage.

Add a Timer to update the UI at intervals, then then add a tick event handler to timer.

using System;

namespace CPURAM
{
    public partial class Form1 : MetroFramework.Forms.MetroForm
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            // Fetch CPU and RAM usage values
            float fcpu = pCPU.NextValue();
            float fram = pRAM.NextValue();
            // Set progress bar values
            metroProgressBarCPU.Value = (int)fcpu;
            metroProgressBarRAM.Value = (int)fram;
            // Update labels with formatted values
            lblCPU.Text = string.Format("{0:0.00}%", fcpu);
            lblRAM.Text = string.Format("{0:0.00}%", fram);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer.Start();
        }
    }
}

When you run the application, it will dynamically update the CPU and RAM usage every second.

VIDEO TUTORIAL