在C#程序,我试图让应用程序的CPU使用率,但它总是显示100 [英] In a C# Program, I am trying to get the CPU usage percentage of the application but it always shows 100

查看:534
本文介绍了在C#程序,我试图让应用程序的CPU使用率,但它总是显示100的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的代码

        PerformanceCounter cpuCounter = new PerformanceCounter();
        cpuCounter.CategoryName = "Processor";
        cpuCounter.CounterName = "% Processor Time";
        cpuCounter.InstanceName = "_Total";

        // will always start at 0
        dynamic firstValue = cpuCounter.NextValue();
        System.Threading.Thread.Sleep(1000);


        dynamic secondValue = cpuCounter.NextValue();
        return secondValue;

有很多人在这里被得到这个返回零的问题,我不过我越来越100在任何时候,我肯定知道我的软件是不是占用了这么多CPU powrer。任何想法,我可以做什么正确地做这项工作。需要明确的是,我尝试从运行此代码不是一个单独的一个应用程序的处理器使用率。另外,我在虚拟机上运行,​​但是这似乎并不重要。任何想法?

There are a lot of people here that are getting the problem that this returns zero, I however am getting 100 at all times, and I know for sure my software is not taking up that much CPU powrer. Any ideas as to what I can do to make this work correctly. To be clear I am trying to retrieve the processor usage from the application running this code not a separate one. Also I am running on a VM but that doesn't seem to matter. Any ideas?

推荐答案

它有助于了解CPU的工作原理弄清楚为什么发生这种情况。我将简化它有点与只有一个核心谈论的CPU,故事并没有一个多核改变。

It helps to understand how a cpu works to figure out why this is happening. I'll simplify it a bit by talking about a cpu with only one core, the story doesn't change for one with multiple cores.

一个的CPU核心可以是仅在两个不同的状态时,它不处于睡眠模式。它可以是执行代码,在全通径为快,因为它可以去执行指令运行。或者是因为没有什么需要做它不执行代码。在这种情况下,它在物理上断开。其中,当执行HLT指令发生。停止,什么也不做,不消耗功率。

A cpu core can be in only two distinct states when it is not in a sleep mode. It is either executing code, running at full bore as fast as it can go executing instructions. Or it is not executing code because nothing needs to be done. In which case it is physically turned off. Which happens when it executes the HLT instruction. Halt, do nothing at all, consume no power.

有只有一个恢复从暂停状态的处理器的方式,需要中断。这就是芯片上的物理引脚,该引脚上的电电压颠簸活着回来。由中断控制器,另一个芯片,需要从机器的各个部分请求得到CPU做一些有用的东西产生的电压。

There is only one way to revive the processor from the Halt state, an interrupt is needed. That's a physical pin on the chip, an electrical voltage on that pin jolts it back alive. That voltage is generated by an interrupt controller, another chip that takes requests from various parts of the machine to get the cpu to do something useful.

这方面最重要的来源中断是一个时钟。它在蜱0.0156秒,颠簸回CPU每秒活着64倍。它激活在操作系统中,基本上检查两件事情的一段代码。 出什么事了,当我睡着了吗?。而我其实一直没有睡过可言,时间做别的,是非常重要的太。或者换句话说,它不只是摇晃CPU活着,这也打断它在它在做什么。因此,信号的名称。

The most important source of that interrupt is a clock. It ticks at 0.0156 seconds, jolting the cpu back alive 64 times per second. Which activates a piece of code in the operating system that basically checks for two things. "Did something happen when I was asleep?". And "I haven't been asleep at all, time to do something else that's important too". Or in other words, it doesn't just jolt the cpu alive, it also interrupts it in what it was doing. Thus the name of the signal.

时钟不中断的唯一来源,你得连接到CPU硬件的其他位。 I / O设备。任何机器具有重要的是盘控制器,光盘驱动器控制器,网卡,鼠标和键盘。和其他任何你在设备管理器小程序见。他们是子系统与可以利用的东西照顾自己的小处理器。并告诉他们完成主CPU。所以,无论你的机器上线程正在等待结果可以恢复与提供的数据执行。

The clock is not the only source of interrupts, you've got other bits of hardware attached to the cpu. I/O devices. The important ones that any machine has are the disk controller, the optical drive controller, the network card, the mouse and the keyboard. And whatever else you see in the Device Manager applet. They are sub-systems with their own little processors that can take care of stuff. And tell the main cpu that they are done. So whatever thread on your machine was waiting for the result can resume executing with the supplied data.

绝大多数的时候,那个人电脑你有坐在旁边你没有做任何事情重要。它是说,等你来阅读这个答案。一些由视频适配器和液晶显示器的照顾,在CPU没有帮助。你可以看到它的TaskMgr.exe,CPU利用率徘徊在0。你移动鼠标,点击滚动条阅读(长)的答案,然后有事。鼠标产生一个中断,即得到通过软件几层过去了,它最终告诉浏览器滚动视图。这大约需要几毫秒的时间,给予或采取。

The vast majority of the time, that personal computer you've got sitting next to you is not doing anything important. It is, say, waiting for you to read this answer. Something taken care of by the video adapter and the LCD monitor, the cpu doesn't have to help. You can see it in TaskMgr.exe, the cpu utilization is hovering around 0. You move the mouse, click on the scrollbar to read this (long) answer, then something happens. The mouse generates an interrupt, that gets passed through several layers of software and it ultimately tells the browser to scroll the view. That takes about a few milliseconds, give or take.

工作的几个毫秒,CPU后回落睡着的权利。所以,当你问的CPU,具有性能计数器,多少工作你最近做的,它会说:好了,我花了几毫秒燃烧的核心和工作我的屁股了更新你的屏幕,但没有其他。比我睡在豌豆

A few milliseconds of work, the cpu falls back asleep right after it. So when you ask the cpu, with the performance counter, "how much work have you done lately", it will say: "well, I spent a couple of milliseconds burning core and working my butt off updating your screen. But no, other than that I was sleeping on the pea"

所以,重要的是你问多久。样板方式是,性能监视器使用的那个。一旦第二个是问你做了什么。并且处理器会说:好了,在最后一秒,我执行的有益的工作20毫秒我死了,世界上剩下的时间里,没有任何有用的。所以,任务管理器显示2%。

So what matters is how often you ask. The boilerplate way is the one that Perfmon uses. Once a second it ask "what have you done". And the processor will say: "well, in the last second I've executed 20 milliseconds of useful work. I was dead the world the rest of the time, nothing useful to do". So task manager displays 2%.

现在可以做你的方式。你问的性能计数器值,那么一纳秒以后你再问吧。该处理器会说:!好吧,我返回性能计数器的值,并试图回去睡觉没有工作,程序不停地奔跑代码,并问我同样的事情

Now lets do it your way. You ask for the perf counter value, then a nanosecond later you ask for it again. The processor will say: "well, I returned a performance counter value and tried to go back to sleep. Didn't work, the program kept running code and asked me the same thing!".

所以结果是准确的,处理器与您的要求被占用,你不给它一个机会断电。 CPU利用率为100%

So the result is accurate, the processor was occupied with your requests, you didn't give it a chance to power down. Cpu utilization was 100%.

解决方法:做这样性能监视器一样。具有定时问一次作为第二。让它更短,数量上升和下降速度更快。让它太短,这将是100%

Solution: do it like Perfmon does. Ask once as second with a timer. Make it shorter and the number goes up and down faster. Make it too short and it will be 100%

这篇关于在C#程序,我试图让应用程序的CPU使用率,但它总是显示100的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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