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:
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
Post a Comment