任务管理器中CPU使用和CPU内核使用有啥区别

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了任务管理器中CPU使用和CPU内核使用有啥区别相关的知识,希望对你有一定的参考价值。

参考技术A 有,颜色不一样,一个是绿色,一个是红色,普通情况下任务管理器只显示CPU的使用情况,CPU内核使用情况是隐藏的。想看CPU内核使用情况的话:打开任务管理器,选到“性能”标签,再点“查看”菜单,是不是看到“显示内核时间”菜单前有了一个勾,就是它引起的了,这是一个开关型菜单。去掉这个勾的时候CPU显示的就只是绿色了,有这个勾的时候就会显示红色和绿色了,红色代表的就是你CPU当前的内核处理性能。

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();
        }
    }
}

以上是关于任务管理器中CPU使用和CPU内核使用有啥区别的主要内容,如果未能解决你的问题,请参考以下文章

在任务管理器中显示所有CPU内核性能

esxi6.7 为啥虚拟机多核cpu 任务管理器中只显示两个cpu

windows任务管理器中查看选项中的“显示内核时间”是啥意思?啥是“内核时间”?

如何在 任务管理器中 查看某一个进程的cpu占用率?

任务管理器中CPU不见了

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