知屋网

当前位置:首页 > 数码网络

C#获取特定进程CPU和内存使用率

作者:opple 时间:2024-04-13 阅读:()

  /// 获取物理内存 ///

  public long PhysicalMemory { get { return m_PhysicalMemory; } } #endregion #region 结束指定进程 ///

  /// 结束指定进程 ///

  ///

  进程的 Process IDpublic static void EndProcess(int pid) { try { Process process = Process.GetProcessById(pid); process.Kill(); } catch { } } #endregion #region 查找所有应用程序标题 ///

  /// 查找所有应用程序标题 ///

  ///

  应用程序标题范型

  public static List

  FindAllApps(int Handle) { ListApps = new List(); int hwCurr; hwCurr = GetWindow(Handle, GW_HWNDFIRST); while (hwCurr > 0) { int IsTask = (WS_VISIBLE | WS_BORDER); int lngStyle = GetWindowLongA(hwCurr, GWL_STYLE); bool TaskWindow = ((lngStyle & IsTask) == IsTask); if (TaskWindow) { int length = GetWindowTextLength(new IntPtr(hwCurr)); StringBuilder sb = new StringBuilder(2 * length + 1); GetWindowText(hwCurr, sb, sb.Capacity); string strTitle = sb.ToString(); if (!string.IsNullOrEmpty(strTitle)) { Apps.Add(strTitle); } } hwCurr = GetWindow(hwCurr, GW_HWNDNEXT); } return Apps; } #endregion }}

  然后是执行代码:

  using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Diagnostics;using System.Threading;namespace CSharpPerformance{//该程序可以实时监控程序本身对应进程的工作集、私有工作集和CPU使用率 class Program { static void Main(string[] args) { //获取当前进程对象 Process cur = Process.GetCurrentProcess(); PerformanceCounter curpcp = new PerformanceCounter("Process", "Working Set - Private", cur.ProcessName); PerformanceCounter curpc = new PerformanceCounter("Process", "Working Set", cur.ProcessName); PerformanceCounter curtime = new PerformanceCounter("Process", "% Processor Time", cur.ProcessName); //上次记录CPU的时间 TimeSpan prevCpuTime = TimeSpan.Zero; //Sleep的时间间隔 int interval = 1000; PerformanceCounter totalcpu = new PerformanceCounter("Processor", "% Processor Time", "_Total"); SystemInfo sys = new SystemInfo(); const int KB_DIV = 1024; const int MB_DIV = 1024 * 1024; const int GB_DIV = 1024 * 1024 * 1024; while (true) { //第一种方法计算CPU使用率 //当前时间 TimeSpan curCpuTime = cur.TotalProcessorTime; //计算 double value = (curCpuTime - prevCpuTime).TotalMilliseconds / interval / Environment.ProcessorCount * 100; prevCpuTime = curCpuTime; Console.WriteLine("{0}:{1} {2:N}KB CPU使用率:{3}", cur.ProcessName, "工作集(进程类)", cur.WorkingSet64 / 1024,value);//这个工作集只是在一开始初始化,后期不变 Console.WriteLine("{0}:{1} {2:N}KB CPU使用率:{3}", cur.ProcessName, "工作集 ", curpc.NextValue() / 1024,value);//这个工作集是动态更新的 //第二种计算CPU使用率的方法 Console.WriteLine("{0}:{1} {2:N}KB CPU使用率:{3}%", cur.ProcessName, "私有工作集 ", curpcp.NextValue() / 1024,curtime.NextValue()/Environment.ProcessorCount); //Thread.Sleep(interval); //第一种方法获取系统CPU使用情况 Console.Write("r系统CPU使用率:{0}%", totalcpu.NextValue()); //Thread.Sleep(interval); //第二章方法获取系统CPU和内存使用情况 Console.Write("r系统CPU使用率:{0}%,系统内存使用大小:{1}MB({2}GB)", sys.CpuLoad, (sys.PhysicalMemory - sys.MemoryAvailable) / MB_DIV, (sys.PhysicalMemory - sys.MemoryAvailable) / (double)GB_DIV); Thread.Sleep(interval); } Console.ReadLine(); } }}

  以上程序可以正常运行,没隔1S刷新一次,实现动态显示本程序对应进程的CPU和内存使用情况。

显示全部

收起

相关文章
精品推荐
猜你喜欢