如何在C#winforms中获取系统的CPU使用率和内存使用率?

编程入门 行业动态 更新时间:2024-10-28 14:30:58
本文介绍了如何在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使用率和内存使用率?

本文发布于:2023-11-16 16:48:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1606079.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:使用率   内存   如何在   系统   winforms

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!