当前位置:编程学习 > C#/ASP.NET >>

c# 检测cpu使用率

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

//引用2命名空间
using System.Diagnostics;
using System.Threading;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Process[] p = Process.GetProcessesByName("devenv");//获取指定进程信息
            // Process[] p = Process.GetProcesses();//获取所有进程信息
            string cpu = string.Empty;
            string info = string.Empty;

            PerformanceCounter pp = new PerformanceCounter();//性能计数器
            pp.CategoryName = "Process";//指定获取计算机进程信息  如果传Processor参数代表查询计算机CPU
            pp.CounterName = "% Processor Time";//占有率
            //如果pp.CategoryName="Processor",那么你这里赋值这个参数 pp.InstanceName = "_Total"代表查询本计算机的总CPU。
            pp.InstanceName = "devenv";//指定进程
            pp.MachineName = ".";
            if (p.Length > 0)
            {
                foreach (Process pr in p)
                {
                    while (true)//1秒钟读取一次CPU占有率。
                    {
                        info = pr.ProcessName + "内存:" +

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,