0

..is anyone knows how to get the cpu usage using c-sharp???
I got difficulties to find the tutorials of it..!
can you help me whats the code of it!!!!

tnx...

2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by dheeraj.singh25
0

..is anyone knows how to get the cpu usage using c-sharp???
I got difficulties to find the tutorials of it..!
can you help me whats the code of it!!!!

tnx...

ManagementScope scope = new ManagementScope("root\
\CIMV2");
scope.Options.Impersonation =
ImpersonationLevel.Impersonate;

ManagementObject obj = new ManagementObject();

string strIdle = "Idle";
char b = (char)34; //Double Quotes

string stPath =
"Win32_PerfFormattedData_PerfProc_Process.Name=" + b + strIdle + b;
ManagementPath path = new ManagementPath(stPath);

obj.Path = path;
obj.Scope = scope;
obj.Get();

Debug.Print(obj.Properties["PercentProcessorTime"].Value.ToString ());

I have given this in a while loop to print System Idle Time. But I am
getting 0 always. I searched in google to find something that will
help. I came through the following script.

function abc(x)
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")

Set PerfProcess = objWMIService.Get(_
"Win32_PerfFormattedData_PerfProc_Process.Name='idle'")

Dim i
i = 0
While ( i < 20)
PerfProcess.Refresh_
Wscript.Echo PerfProcess.PercentProcessorTime
Wscript.Sleep 1000
i = i+1
Wend
end function
this is in vb.net u can apply it on c#.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.