如何在C#winforms中获取系统的CPU使用率和内存使用率? [英] How to get cpu usage and memory usage of the system in C# winforms?

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

问题描述

在我的应用程序中,我想在每次1秒的间隔中显示cpu和内存使用情况。请帮我详细说明。



我尝试过:



iam使用PerformanceCounter来获取此信息,但它与系统的cpu使用情况不一致。请找到我的应用程序中使用的代码。



PerformanceCounter cpuCounter;

cpuCounter = new PerformanceCounter(Processor,%特权时间,_总计);

lbl_cpu_usage.Text = Convert.ToInt32(cpuCounter.NextValue())。ToString()+%;(此代码写在计时器刻度事件中。

In my application i want to display cpu and memory usage in every time intervel of 1 second. Please help me to get exact details.

What I have tried:

iam using PerformanceCounter to get this information, but it is not correct with cpu usage of system. Please find the the code that are used in my application.

PerformanceCounter cpuCounter;
cpuCounter = new PerformanceCounter("Processor", "% Privileged Time", "_Total");
lbl_cpu_usage.Text = Convert.ToInt32(cpuCounter.NextValue()).ToString() + "%";(This code is written in the timer tick event.

推荐答案

性能计数器对象通常需要两个相隔1到2秒采样的值才能给出准确的读数..虽然我讨厌我的事实在这里使用线程延迟,我想不出更简单的方法 - 看到我添加到你的代码中的两行粗体 - 看看是否使它更准确



Performance counter objects in general need two values sampled at 1 - 2 seconds apart to be able to give accurate readings .. although I hate the fact I'm using a thread delay here, I can't think of a simpler way - see the two lines in bold I've added to your code - see if that makes it more accurate

PerformanceCounter cpuCounter;
cpuCounter = new PerformanceCounter("Processor", "% Privileged Time", "_Total");
cpuCounter.NextValue();  
System.Threading.Thread.Sleep(1000); 
lbl_cpu_usage.Text = Convert.ToInt32(cpuCounter.NextValue()).ToString() + "%";





[edit] ref:表演计数器价值检索 [ ^ ] [/ edit]



[edit] ref : Performance Counter Value Retrieval[^] [/edit]


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

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