隐藏

如何获取windows总的CPU使用率(包括所有进程)?

发布:2022/11/10 10:34:59作者:管理员 来源:本站 浏览次数:693

        static void Main(string[] args)
        {
            Console.WriteLine("查看进程CPU使用情况");
            system_cpu();
            Console.ReadKey();
        }

        /// <summary>

        /// 创建性能计数器
        /// </summary>
        /// <param name="pname"></param>
        static void system_cpu()
        {
            using (PerformanceCounter system_cpu_usage = new PerformanceCounter("Processor", "% Processor Time", "_Total"))
            {
                
                while (true)
                {
                    var first = system_cpu_usage.NextValue();
                    //注意除以CPU数量
                    Console.WriteLine(first + "%");
                    Thread.Sleep(1000);
                }
            }
            
        }