get current CPU utilization in C#

前端 未结 2 1325
北荒
北荒 2021-02-08 04:36

I want to have current CPU utilization in my project

namespace Monitoring_Tool
{
    public partial class ajaxExecute : System.Web.UI.Page
    {
        private          


        
2条回答
  •  滥情空心
    2021-02-08 05:24

    It is a performance counter that heavily depends on when you read it. A processor core is either executing code, running full bore at "100%". Or it is turned off completely, stopped by the HALT instruction. The "% Processor Time" counter tells you for how many % of the time, since the last time you checked it, it has been executing code.

    So you will only get meaningful values if you wait long enough between sampling it. One second is boilerplate, that's what you see in Perfmon.exe and Taskmgr.exe. The shorter the interval, the less accurate the number gets. It starts to vary wildly, jumping between 0 and 100%.

    So getting 0% in the page's Load event is normal, it just initializes the counter to set the start of the interval. Getting the next sample is hardship, you can't realistically do this in the event handler. You'd have to do something drastic like having a separate timer or thread that samples at one second intervals and use that value.

提交回复
热议问题