jQuery and ASP.NET

October 30, 2009

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



'Like' us on our FaceBook page if you find this blog useful. Thanks!


Did you like this post?
kick it on DotNetKicks.com Save on Delicious
subscribe via rss subscribe via e-mail
print this post follow me on twitter


About The Author

Suprotim Agarwal, ASP.NET Architecture MVP 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 has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

comments

1 Response to "How to calculate the CPU Usage Programmatically using C# or VB.NET"
  1. dips said...
    December 21, 2009 8:49 PM

    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();

 

Copyright © 2009-2011 All Rights Reserved for DevCurry.com by Suprotim Agarwal | Terms and Conditions