..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...

..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#.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.