C # cpu usage Detection

Source: Internet
Author: User

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;

// Reference 2 namespace
Using System. Diagnostics;
Using System. Threading;

Namespace ConsoleApplication1
{
Class Program
{
Static void Main (string [] args)
{
Process [] p = Process. GetProcessesByName ("devenv"); // obtain the information of a specified Process
// Process [] p = Process. GetProcesses (); // obtain information about all processes
String cpu = string. Empty;
String info = string. Empty;

PerformanceCounter pp = new PerformanceCounter (); // Performance Counter
Pp. CategoryName = "Process"; // specify to obtain computer Process information. If the Processor parameter is passed, the system queries the computer CPU.
Pp. CounterName = "% Processor Time"; // shares
// If pp. CategoryName = "Processor", you can assign this parameter pp. InstanceName = "_ Total" Here to query the Total CPU of the computer.
Pp. InstanceName = "devenv"; // specifies the process
Pp. MachineName = ".";
If (p. Length> 0)
{
Foreach (Process pr in p)
{
While (true) // read the CPU usage once per second.
{
Info = pr. ProcessName + "memory:" +

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.