How to calculate the CPU Usage Programmatically using C# or VB.NET

The PerformanceCounter class represents a WindowsNT performance counter component. To read from a performance counter, we first create an instance of the PerformanceCounter class supplying the CategoryName, CounterName, and InstanceName as parameters to the constructor and then call the NextValue() method to take a performance counter reading.

Import the namespaces System.Diagnostics and System.Threading.

C#

static PerformanceCounter cpuUsage;
public static void Main(string[] args)
{
cpuUsage =
new PerformanceCounter("Processor", "% Processor Time", "_Total");

Console.WriteLine(cpuUsage.NextValue() + " %");
Thread.Sleep(1000);
Console.WriteLine(cpuUsage.NextValue() + " %");
Console.Read();
}

VB.NET

Private Shared cpuUsage As PerformanceCounter
Public Shared Sub Main(ByVal args() As String)
cpuUsage = _
New PerformanceCounter("Processor", "% Processor Time", "_Total")

Console.WriteLine(cpuUsage.NextValue() & " %")
Thread.Sleep(1000)
Console.WriteLine(cpuUsage.NextValue() & " %")
Console.Read()
End Sub

Note: Observe that we are calling NextValues() twice and after a delay of 1 second. This is because performance counters need two samples to perform the calculation and the counter value is updated once per second, hence the 1 second delay between the two calls.

The output on running this code is as shown below:

image



Will you give this article a +1 ? Thanks in advance


About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal

1 comment:

dips said...

Hello

Im new to dot net.
Please help me. I'm getting following error

Access to the registry key 'Global' is denied.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.UnauthorizedAccessException: Access to the registry key 'Global' is denied.

ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6) that is used if the application is not impersonating. If the application is impersonating via , the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user.

To grant ASP.NET access to a file, right-click the file in Explorer, choose "Properties" and select the Security tab. Click "Add" to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access.

Source Error:

Line 16: protected void Get_CPU()
Line 17: {
Line 18: cpuUsage =
Line 19: new PerformanceCounter("Processor", "% Processor Time", "_Total");
Line 20: cpuUsage.NextValue();