免费无码作爱视频,女厕厕露p撒尿八个少妇,精品人妻av区乱码,国产aⅴ爽av久久久久久

C#獲取特定進程CPU和內存使用率

時間:2022-03-30 04:49:22 電腦 我要投稿
  • 相關推薦

C#獲取特定進程CPU和內存使用率

  首先是獲取特定進程對象,可以使用Process.GetProcesses()方法來獲取系統中運行的所有進程,或者使用Process.GetCurrentProcess()方法來獲取當前程序所對應的進程對象。當有了進程對象后,可以通過進程對象名稱來創建PerformanceCounter類型對象,通過設定PerformanceCounter構造函數的參數實現獲取特定進程的CPU和內存使用情況。

  具體實例代碼如下:

  首先是獲取本機中所有進程對象,分別輸出某一時刻各個進程的內存使用情況:

  using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Diagnostics;using System.Threading;namespace CSharpPerformance{//該程序可以實時監控所有進程或者指定進程的工作集、私有工作集 class Program { static void Main(string[] args) { //新建一個Stopwatch變量用來統計程序運行時間 Stopwatch watch = Stopwatch.StartNew(); //獲取本機運行的所有進程ID和進程名,并輸出哥進程所使用的工作集和私有工作集 foreach (Process ps in Process.GetProcesses()) { PerformanceCounter pf1 = new PerformanceCounter("Process", "Working Set - Private", ps.ProcessName); PerformanceCounter pf2 = new PerformanceCounter("Process", "Working Set", ps.ProcessName); Console.WriteLine("{0}:{1} {2:N}KB", ps.ProcessName, "工作集(進程類)", ps.WorkingSet64 / 1024); Console.WriteLine("{0}:{1} {2:N}KB", ps.ProcessName, "工作集 ", pf2.NextValue() / 1024); //私有工作集 Console.WriteLine("{0}:{1} {2:N}KB", ps.ProcessName, "私有工作集 ", pf1.NextValue() / 1024); } watch.Stop(); Console.WriteLine(watch.Elapsed); Console.ReadLine(); } }}

  其中,工作集ps.WorkingSet64是靜態的,pf2.NextValue()是動態變化的,工作集包含進程運行時其獨占的內存和與其他進程共享的內存的和,而私有工作集是只包含進程獨占的內存。

  下面一組代碼可以動態顯示本程序所對應的進程的CPU和內存使用率的變化:

  首先是SystemInfo.cs類:

  using System;using System.Collections.Generic;using System.Diagnostics;using System.Threading;using System.IO;using System.Text;using System.Management;using System.Runtime.InteropServices;namespace CSharpPerformance{ public class SystemInfo { private int m_ProcessorCount = 0; //CPU個數 private PerformanceCounter pcCpuLoad; //CPU計數器 private long m_PhysicalMemory = 0; //物理內存 private const int GW_HWNDFIRST = 0; private const int GW_HWNDNEXT = 2; private const int GWL_STYLE = (-16); private const int WS_VISIBLE = 268435456; private const int WS_BORDER = 8388608; #region AIP聲明 [DllImport("IpHlpApi.dll")] extern static public uint GetIfTable(byte[] pIfTable, ref uint pdwSize, bool bOrder); [DllImport("User32")] private extern static int GetWindow(int hWnd, int wCmd); [DllImport("User32")] private extern static int GetWindowLongA(int hWnd, int wIndx); [DllImport("user32.dll")] private static extern bool GetWindowText(int hWnd, StringBuilder title, int maxBufSize); [DllImport("user32", CharSet = CharSet.Auto)] private extern static int GetWindowTextLength(IntPtr hWnd); #endregion #region 構造函數 ///

  /// 構造函數,初始化計數器等 ///

  public SystemInfo() { //初始化CPU計數器 pcCpuLoad = new PerformanceCounter("Processor", "% Processor Time", "_Total"); pcCpuLoad.MachineName = "."; pcCpuLoad.NextValue(); //CPU個數 m_ProcessorCount = Environment.ProcessorCount; //獲得物理內存 ManagementClass mc = new ManagementClass("Win32_ComputerSystem"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { if (mo["TotalPhysicalMemory"] != null) { m_PhysicalMemory = long.Parse(mo["TotalPhysicalMemory"].ToString()); } } } #endregion #region CPU個數 ///

  /// 獲取CPU個數 ///

  public int ProcessorCount { get { return m_ProcessorCount; } } #endregion #region CPU占用率 ///

  /// 獲取CPU占用率 ///

  public float CpuLoad { get { return pcCpuLoad.NextValue(); } } #endregion #region 可用內存 ///

  /// 獲取可用內存 ///

  public long MemoryAvailable { get { long availablebytes = 0; //ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT * FROM Win32_PerfRawData_PerfOS_Memory"); //foreach (ManagementObject mo in mos.Get()) //{ // availablebytes = long.Parse(mo["Availablebytes"].ToString()); //} ManagementClass mos = new ManagementClass("Win32_OperatingSystem"); foreach (ManagementObject mo in mos.GetInstances()) { if (mo["FreePhysicalMemory"] != null) { availablebytes = 1024 * long.Parse(mo["FreePhysicalMemory"].ToString()); } } return availablebytes; } } #endregion #region 物理內存 ///

  /// 獲取物理內存 ///

  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和內存使用情況。

【C#獲取特定進程CPU和內存使用率】相關文章:

Explorer.exe進程的CPU使用率為100%如何解決04-01

獲取CPU的溫度的方法06-03

CPU使用率高的原因03-27

CPU使用率占用案例03-31

電腦CPU使用率100%怎么解決04-02

獲取電腦最大支持內存方法06-03

電腦內存使用率過高怎么加速04-02

的方法深入c#詳解內存管理以及優化05-01

關于Mac的cpu使用率查看的詳細教程06-04

電腦cpu使用率過高的解決辦法04-02

主站蜘蛛池模板: 海阳市| 莲花县| 老河口市| 永济市| 孝感市| 旬阳县| 上栗县| 宝坻区| 靖江市| 东安县| 沧源| 大城县| 榆中县| 孝昌县| 桑日县| 晋州市| 田阳县| 天祝| 望奎县| 新乐市| 章丘市| 高要市| 惠安县| 阿坝| 长武县| 灯塔市| 本溪市| 吉木萨尔县| 济源市| 石阡县| 边坝县| 灵台县| 阳朔县| 南皮县| 西平县| 顺昌县| 邵阳市| 利辛县| 原平市| 化州市| 游戏|