. Net (C #): monitor CPU usage

Source: Internet
Author: User

Directory

  • 1. CPU user time and Kernel Time of the process
  • 2. CPU usage statistics
  • 3. Download source code

 

Returned directory

1. CPU user time and Kernel Time of the process

The userprocessortime and privilegedprocessortime attributes of the Process class can be used to return the CPU user and Kernel Time consumed by the current process. Process. totalprocessortime indicates the sum of the two. Both return the timespan struct object.

 

With these three attributes, you can make a simple program to monitor the CPU time of the process.

As follows: (of course, when the CPU is not very slow, a significant increase in CPU time will not be seen after an operation)

 

 

 

 

Returned directory

2. CPU usage statistics

The simplest way to count the usage of all CPUs is to use the Windows Task Manager program. By default, however, only the percentage of "non-idle" execution time is displayed. In the Windows 7 Task Manager view, the "display Kernel Time" option is displayed. In this way, the user time and Kernel Time are separated. The kernel time is in red, for example:

 

 

In programming, you can use performance counters for more detailed CPU time monitoring. For example, the following is a program that monitors CPU usage:

 

 

The performance counter used is in the processor class:

  • % Idle time: idle
  • % Interrupt time: Indicates hardware interruption.
  • % USER time: Indicates processing user commands.
  • % Privileged time: indicates the operating system kernel command
  • % Processor time: indicates all non-idle time (also displayed by the task manager by default)

(Percentage of all the above performance counters returned !)

 

You can view this information in the Performance Monitor of the Control Panel (or type: perfmon during running)

 

 

The last thing to note is the transaction macecounter class (system. while the getnextvalue method in the namespace of diagnostics returns float, the percentage is normalized from 0.0 to 100.0, instead of 0.0 to 1.0.

 

Returned directory

3. Download source code

Download the current version of the program and source code

Download Page

Note: the link is the Microsoft SkyDrive page. When downloading, use a browser to download it directly. Some download tools may not be available for downloading.

Source code environment: Visual C # express 2010

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.