winforms - Find CPU Usage at the moment C# -


i have developed following code sample using online example.but didn't give me percentage of cpu usage.

    performancecounter rcpucounter;     performancecounter rramcounter;      public void resourcescpu()     {         rcpucounter = new performancecounter();          rcpucounter.categoryname = "processor";         rcpucounter.countername = "% processor time";         rcpucounter.instancename = "_total";          string cpu = getcurrentcpuusage();     }      public string getcurrentcpuusage()     {         string value1 = rcpucounter.nextvalue() + "%";          return value1;     } 

and then, changed getcurrentcpuusage() method below.it gave me value cpu usage.

public string getcurrentcpuusage()     {         string value1 = rcpucounter.nextvalue() + "%";         thread.sleep(500);         string value2 = rcpucounter.nextvalue() + "%";          return value2;     } 

i know possible have different cpu usage in different time.but question why didn't give me value in first algorithm.

the answer in msdn:

https://msdn.microsoft.com/en-us/library/system.diagnostics.performancecounter.nextvalue(v=vs.110).aspx

if calculated value of counter depends on 2 counter reads, first read operation returns 0.0. resetting performance counter properties specify different counter equivalent creating new performance counter, , first read operation using new properties returns 0.0. recommended delay time between calls nextvalue method 1 second, allow counter perform next incremental read.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -