• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

c# 获取进程cpu的使用率 试验

武飞扬头像
科学的发展-只不过是读大自然写的代码
帮助1

1.概要

想了解程序的效率,和运行的风险,准备观察一个CPU和内存的占用率。

2.代码

  1.  
    using System;
  2.  
    using System.Diagnostics;
  3.  
    using System.Threading;
  4.  
     
  5.  
    namespace 获取进程cpu的使用率
  6.  
    {
  7.  
    class Program
  8.  
    {
  9.  
    static void Main(string[] args)
  10.  
    {
  11.  
    Console.WriteLine();
  12.  
    //获取当前进程对象
  13.  
    Process cur = Process.GetCurrentProcess();
  14.  
     
  15.  
    PerformanceCounter curpcp = new PerformanceCounter("Process", "Working Set - Private", cur.ProcessName);
  16.  
    PerformanceCounter curpc = new PerformanceCounter("Process", "Working Set", cur.ProcessName);
  17.  
    PerformanceCounter curtime = new PerformanceCounter("Process", "% Processor Time", cur.ProcessName);
  18.  
     
  19.  
    //上次记录CPU的时间
  20.  
    TimeSpan prevCpuTime = TimeSpan.Zero;
  21.  
    //Sleep的时间间隔
  22.  
    int interval = 1000;
  23.  
     
  24.  
    PerformanceCounter totalcpu = new PerformanceCounter("Processor", "% Processor Time", "_Total");
  25.  
     
  26.  
    SystemInfo sys = new SystemInfo();
  27.  
    const int KB_DIV = 1024;
  28.  
    const int MB_DIV = 1024 * 1024;
  29.  
    const int GB_DIV = 1024 * 1024 * 1024;
  30.  
    while (true)
  31.  
    {
  32.  
    //第一种方法计算CPU使用率
  33.  
    //当前时间
  34.  
    TimeSpan curCpuTime = cur.TotalProcessorTime;
  35.  
    //计算
  36.  
    double value = (curCpuTime - prevCpuTime).TotalMilliseconds / interval / Environment.ProcessorCount * 100;
  37.  
    prevCpuTime = curCpuTime;
  38.  
     
  39.  
    Console.WriteLine("{0}:{1} {2:N}KB CPU使用率:{3}", cur.ProcessName, "工作集(进程类)", cur.WorkingSet64 / 1024, value);//这个工作集只是在一开始初始化,后期不变
  40.  
    Console.WriteLine("{0}:{1} {2:N}KB CPU使用率:{3}", cur.ProcessName, "工作集 ", curpc.NextValue() / 1024, value);//这个工作集是动态更新的
  41.  
    //第二种计算CPU使用率的方法
  42.  
    Console.WriteLine("{0}:{1} {2:N}KB CPU使用率:{3}%", cur.ProcessName, "私有工作集 ", curpcp.NextValue() / 1024, curtime.NextValue() / Environment.ProcessorCount);
  43.  
    //Thread.Sleep(interval);
  44.  
     
  45.  
    //第一种方法获取系统CPU使用情况
  46.  
    Console.Write("\r系统CPU使用率:{0}%", totalcpu.NextValue());
  47.  
    //Thread.Sleep(interval);
  48.  
     
  49.  
    //第二章方法获取系统CPU和内存使用情况
  50.  
    Console.Write("\r系统CPU使用率:{0}%,系统内存使用大小:{1}MB({2}GB)", sys.CpuLoad, (sys.PhysicalMemory - sys.MemoryAvailable) / MB_DIV, (sys.PhysicalMemory - sys.MemoryAvailable) / (double)GB_DIV);
  51.  
    Thread.Sleep(interval);
  52.  
    }
  53.  
    Console.ReadKey();
  54.  
    }
  55.  
    }
  56.  
     
  57.  
    }
  58.  
     
  59.  
     
学新通

3.运行结果

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgfkbhf
系列文章
更多 icon
同类精品
更多 icon
继续加载