如何在 C# 中获取 CPU 使用率? [英] How to get the CPU Usage in C#?

查看:67
本文介绍了如何在 C# 中获取 CPU 使用率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 C# 中获取应用程序的总体 CPU 使用率.我找到了很多方法来挖掘进程的属性,但我只想要进程的 CPU 使用率,以及您在 TaskManager 中获得的总 CPU.

I want to get the overall total CPU usage for an application in C#. I've found many ways to dig into the properties of processes, but I only want the CPU usage of the processes, and the total CPU like you get in the TaskManager.

我该怎么做?

推荐答案

您可以使用 PerformanceCounter 类来自 系统诊断.

You can use the PerformanceCounter class from System.Diagnostics.

像这样初始化:

PerformanceCounter cpuCounter;
PerformanceCounter ramCounter;

cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
ramCounter = new PerformanceCounter("Memory", "Available MBytes");

这样消费:

public string getCurrentCpuUsage(){
            return cpuCounter.NextValue()+"%";
}

public string getAvailableRAM(){
            return ramCounter.NextValue()+"MB";
} 

这篇关于如何在 C# 中获取 CPU 使用率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆