zoukankan      html  css  js  c++  java
  • c# 获取某个进程的CPU使用百分百(类似任务管理器中显示CPU)

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Management;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;

    namespace 进程监控
    {
        class Program
        {
            static void Main(string[] args)
            {
                Process[] processes = Process.GetProcessesByName("taskmgr");
               
                foreach (Process instance in processes)
                {
                    Console.WriteLine("");
                    Console.WriteLine("ProcessName:" + instance.ProcessName);
                    try
                    {
                        //Console.WriteLine("提交大小 " + instance.PagedMemorySize64 / 1024);
                        Console.WriteLine("工作设置(内存) " + instance.WorkingSet64 / 1024);
                        Console.WriteLine("线程数 " + instance.Threads.Count);
                        Console.WriteLine("句柄数 " + instance.HandleCount);

                    }
                    catch { }
                }

                Process p = processes[1];
                //PerformanceCounter ramCounter = new PerformanceCounter("Process", "Working Set", p.ProcessName);
                //PerformanceCounter cpuCounter = new PerformanceCounter("Process", "% Processor Time", p.ProcessName);


                var objQuery = new ObjectQuery("select * from Win32_Process WHERE ProcessID = " + p.Id);
                var moSearcher = new ManagementObjectSearcher(objQuery);
                DateTime firstSample = DateTime.MinValue, secondSample = DateTime.MinValue;

                double ProcessorUsage;
                double msPassed;
                ulong u_OldCPU = 0;
                while (true)
                {
                    var gets = moSearcher.Get();
                    foreach (ManagementObject mObj in gets)
                    {
                        try
                        {
                            if (firstSample == DateTime.MinValue)
                            {
                                firstSample = DateTime.Now;
                                mObj.Get();
                                u_OldCPU = (ulong)mObj["UserModeTime"] + (ulong)mObj["KernelModeTime"];
                            }
                            else
                            {
                                secondSample = DateTime.Now;
                                mObj.Get();
                                ulong u_newCPU = (ulong)mObj["UserModeTime"] + (ulong)mObj["KernelModeTime"];

                                msPassed = (secondSample - firstSample).TotalMilliseconds;
                                ProcessorUsage = (u_newCPU - u_OldCPU) / (msPassed * 100.0 * Environment.ProcessorCount);

                                u_OldCPU = u_newCPU;
                                firstSample = secondSample;
                                Console.WriteLine("ProcessorUsage:" + (int)ProcessorUsage);
                            }
                          
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message + ex.StackTrace);
                            Console.WriteLine(ex.InnerException.Message);
                        }
                    }
                    Thread.Sleep(1000);
                }
                Console.ReadLine();
            }
        }
    }

  • 相关阅读:
    WinEdt && LaTex(三)—— 宏包
    矩阵分析相关证明(一) —— 正交与投影
    矩阵分析相关证明(一) —— 正交与投影
    windows 的使用 —— 注册表(软件的安装和卸载)
    windows 的使用 —— 注册表(软件的安装和卸载)
    中英文对照 —— 生化(生物化学)、生理(生物物理)
    中英文对照 —— 生化(生物化学)、生理(生物物理)
    三言二拍
    三言二拍
    一题多解(八)—— 矩阵上三角(下三角)的访问
  • 原文地址:https://www.cnblogs.com/94cool/p/6336593.html
Copyright © 2011-2022 走看看