RE: How to get CPU Usage using C# .Net

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




"soumya.dharmarajan@xxxxxxxxx" wrote:

Hi
I have an C# application in which I am using WMI to obtain CPU
Usage of the total system. I obtain the CPU Usage in % for System Idle
Process and subtract it from 100 to obtain CPU Usage.
Here is the code to obtain CPU Usage

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 script is giving me valid Values. I compared with Task Manager
CPU Usage. Now I tried calling this script from my C# code. I created
a ScriptControlClass instance and called the Run method after Adding
the script to the instance. See the code below


MSScriptControl.ScriptControlClass ms;
ms = new MSScriptControl.ScriptControlClass();
ms.AllowUI = true;
ms.Language = "VBScript";
ms.UseSafeSubset = false;
ms.Reset();

string str = rftScript.Text;//This is obtained from my
Form. I am copying
//script to a
Text Box in my form
object[] i = new object[1];
i[0] = "1";
ms.AddCode(str);
object oRes = ms.Run("abc",ref i );

When I run this piece of code it will give me an Exception "Generic
Failure" where ms.Run is called. I commented out
"PerfProcess.Refresh_" in VBscript and then this piece of code works
without any exception. But the problem is again System Idle Process
percentage of CPU time I am getting is 0.

Can anyone tell me a way to refresh the WMI call from C# ? Or how can
I make this VBscript work from C#? Is there any other way to get
percantage of CPU Usage?
Please help me to solve this issue

Regards
Soumya



There are probably better ways of doing this, but here goes:

using System;
using System.Management;
using System.Threading;
using System.Text;

namespace WMITest
{
class Program
{
static void Main()
{

for (int i = 0; i < 30; i++)
{
ManagementObject processor = new ManagementObject(
"Win32_PerfFormattedData_PerfOS_Processor.Name='_Total'");
processor.Get();

Console.WriteLine(processor.Properties["PercentProcessorTime"].Value);
Thread.Sleep(100); //miliseconds
}
}
}
}


--
urkec


.



Relevant Pages

  • wmi PercentProcessorTime win2k3 problem.
    ... I am trying to monitor CPU usage of the processes in Windows machine. ... //using (ManagementObject mo = new ManagementObject ... // cpuUsg = Convert.ToInt32(mo ...
    (microsoft.public.win32.programmer.wmi)
  • Re: How to get CPU Usage using C# .Net
    ... Process and subtract it from 100 to obtain CPU Usage. ... I came through the following script. ... ManagementObject processor = new ManagementObject( ... ulong u_oldNano = ...
    (microsoft.public.win32.programmer.wmi)
  • How to get CPU Usage using C# .Net
    ... I have an C# application in which I am using WMI to obtain CPU ... Process and subtract it from 100 to obtain CPU Usage. ... ManagementScope scope = new ManagementScope("root\ ... This script is giving me valid Values. ...
    (microsoft.public.win32.programmer.wmi)
  • Re: control CPU usage
    ... The script monitors global input using PyHook, ... My first question is whether the pyHook event is calling the SendKeys ... responsiveness may not be caused by high CPU usage, as the OS, be it ... But theoretically, as soon as you return to the event loop, your main thread is going to block, so it'll schedule the other thread. ...
    (comp.lang.python)
  • Re: a possible bug in expect/tcl: a hypothesis
    ... | monitors the CPU usage of all processes. ... | what is happening to my script. ... See ulimit -t. ... Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org ...
    (comp.lang.tcl)