Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the CPU Usage in asp.net

Is there a way to show CPU and RAM usage statistics on an asp.net page. I've tried this code but I have error:

Access to the registry key 'Global' is denied.

on this line:

ramCounter = new PerformanceCounter("Memory", "Available MBytes");
like image 321
HasanG Avatar asked Mar 18 '11 19:03

HasanG


People also ask

How do I get CPU usage from .NET core?

To get the overall CPU usage, get all processes with Process. GetProcesses() and compute the sum of the processor times.

How do I check my CPU usage?

The CPU usage information is easily accessible in every operating system. In Windows, all you have to do is open the Task Manager. Beneath the “Performance” tab, you'll be able to check how much of the CPU is being utilized at the present moment.

How do I check CPU usage in Visual Studio?

To bring up the window again, click Debug > Windows > Show Diagnostic Tools. You can choose whether to see CPU Usage, Memory Usage, or both, with the Select Tools setting on the toolbar. If you are running Visual Studio Enterprise, you can also enable or disable IntelliTrace in Tools > Options > IntelliTrace.


2 Answers

As mentioned in comments, without the appropriate permissions you will not be able to do this. Resource statistics will include a lot of information about processes owned by other users of the system which is privileged information

like image 159
squillman Avatar answered Nov 03 '22 02:11

squillman


Use:

    System.Diagnostics.PerformanceCounter cpuUsage = 
      new System.Diagnostics.PerformanceCounter();
    cpuUsage.CategoryName = "Processor"; 
    cpuUsage.CounterName = "% Processor Time";
    cpuUsage.InstanceName = "_Total";

    float f = cpuUsage.NextValue();

Edit:

Windows limits access to the performance counters to those in the Administrators or Performance Logs Users (in Vista+) groups. Setting registry security won't resolve this. It is possible that there is a user right attached to it somewhere.

like image 26
ukhardy Avatar answered Nov 03 '22 02:11

ukhardy