c# 获取某个进程的CPU使用百分百(类似任务管理器中显示CPU)

Posted 吃喝玩乐

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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("提交大小\t" + instance.PagedMemorySize64 / 1024);
                    Console.WriteLine("工作设置(内存)\t" + instance.WorkingSet64 / 1024);
                    Console.WriteLine("线程数\t" + instance.Threads.Count);
                    Console.WriteLine("句柄数\t" + 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();
        }
    }
}

以上是关于c# 获取某个进程的CPU使用百分百(类似任务管理器中显示CPU)的主要内容,如果未能解决你的问题,请参考以下文章

CPU占用100%

如何获取某个进程的cpu占用率

CPU使用率百分之百的问题怎么解决

linux 如何查看cpu已经占用多少百分比

进程占用百分百CPU不卡(从未试过,当别的程序运行的时候,当前程序还会运行吗?)

任务管理器的用途和各系统进程的具体作用?