Someone please provide code for displaying current CPU Utilization on a Progress Bar of a Windows Form. I have tried with solutions present on this site but getting so many errors. Have no idea what is wrong with the code as I am a newbie in C#. I will pick up from there for rest of my programming because i want to restart a specific windows process after that.
Thanks a Lot
Post I have tried:
How to get the CPU Usage in C#? Need C# code for showing CPU Utilization in Progress Bar of Windows Form Application>progress-bar-of-windows-form-ap
I studied about PerformanceCounter and System.Diagnostics but I am unable to code them properly.
Sorry for my 0.1% knowledge in C#.
==============================================================================
My Code:
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 CPU_Utilization_Monitor
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int totalHits = 0;
public object getCPUCOunter()
{
PerformanceCounter cpuCounter = new PerformanceCounter();
cpuCounter.CategoryName = "Processor";
cpuCounter.CounterName = "% Processor Time";
cpuCounter.InstanceName = "_Total";
// will always start at 0
dynamic firstValue = cpuCounter.NextValue();
System.Threading.Thread.Sleep(1000);
// now matches task manager reading
dynamic secondValue = cpuCounter.NextValue();
return secondValue;
}
private void Timer1_Tick(System.Object sender, System.EventArgs e)
{
int cpuPercent = (int)getCPUCOunter();
if (cpuPercent >= 90)
{
totalHits = totalHits + 1;
if (totalHits == 60)
MessageBox.Show("ALERT 90% usage for 1 minute");
totalHits = 0;
}
else
{
totalHits = 0;
}
label1.Text = cpuPercent + " % CPU";
label3.Text = totalHits + " seconds over 20% usage";
}
}
}
Now what I'm getting is:
Error 1 Program 'C:\Users\SnowFlake\Documents\Visual Studio 2010\Projects\CPU Utilization Monitor\CPU Utilization Monitor\obj\x86\Debug\CPU Utilization Monitor.exe' does not contain a static 'Main' method suitable for an entry point CPU Utilization Monitor <