I would like to find out the current CPU usage using the codes below(c# in asp.net framework). However it gives me "0% of CPU usage" when I try to run the program. When I check my task manager, I found out that the actual total CPU usage is more than 5%. Does anyone know what is wrong with the code below?
public partial class cpuUsage : System.Web.UI.Page { PerformanceCounter cpu; protected void Page_Load(object sender, EventArgs e) { cpu = new PerformanceCounter(); cpu.CategoryName = "Processor"; cpu.CounterName = "% Processor Time"; cpu.InstanceName = "_Total"; lblCPUUsage.Text = getCurrentCpuUsage(); } public string getCurrentCpuUsage() { return cpu.NextValue() + "%"; } }