I want to get the cpu usage of the pc overall usage currently each time lets say every 5 seconds to get updated value of the cpu usage.
And every 5 second to get the value of specific process name cpu usage. For example i will choose in runtime what process to get the cpu usage of.
So in the Form1 when im running the program i will see two labels that are updating every 5 seconds.
This is my code now:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace CpuUsage
{
public partial class Form1 : Form
{
private PerformanceCounter theCPUCounter;
private PerformanceCounter theMemCounter;
private PerformanceCounter specProcessCPUCounter;
private float cpuUsage;
private float memUsage;
private string processname;
public Form1()
{
InitializeComponent();
theCPUCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
theMemCounter = new PerformanceCounter("Memory", "Available MBytes");
specProcessCPUCounter = new PerformanceCounter("Process", "% Processor Time", Process.GetCurrentProcess().ProcessName);
processname = specProcessCPUCounter.CounterName;
cpuUsage = this.theCPUCounter.NextValue();
memUsage = theMemCounter.NextValue();
label1.Text = memUsage.ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
I have label1 and label2 in designer.
The value theCPUCounter is 0 all the time.
And i dont know how ot use the specProcessCPUCounter to get the specific process cpuUsage.
And i dont know how to make that they will get update every 5 seconds.
You need to add a WinForms timer and update the label texts in its
Tickhandler.