Does anyone know how to get CPU usage? And if so can I obtain it from
another machine on the network?
Scott O
I am using the following code to get the CPU usage
PerformanceCounter myCounter;
myCounter = new PerformanceCounter();
myCounter.CategoryName = "Processor";
myCounter.CounterName = "% Processor Time";
myCounter.InstanceName = "_Total";
for(int i=0; i < 20; i++)
myCounter.NextValue();
I ran this progam i am seeing values 0 or 100%, but when i see the task
manager the CPU usage is 6 to 22 %.
Am i missing something
--
Sirisha
3. How to get CPU Usage using C# .Net
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
4. how to decrease the cpu usage - CSharp/C#
5. The cpu usage is increased between com and atl
hello everyone.
i made a singleton com with sink.
then i use the com in C# with sink.
-- win32dll -> atl com -> event of atl(if i don't use this code, it's ok) ->
C#
so i wrote my sample code below.
1.the client call the function of com in thread.
2.the function in COM will fire to C#
then the cpu usage is increased.
how to solve this kind of problem.
i have to call the function of com continuely,
do you have any idea to solve this kind of problem.
please , teach me ...
-- Client Side -
i called a function of com from client in thread.
SetValue("AAA", "BBB", __Int, chBuffer);
SetValue("BBB", "CCC", __Int, chBuffer);
-- com side --
SetValue(BSTR A, BSTR B, int i, BSTR C)
{
Fire_Event();
}
-- C# Code
m_easyBridege.OnSetValue += new
_IEasyBridgeEvents_OnSet(easyBridege_OnSetEventId);
void easyBridege_OnSetEventId(string A, string B, int i , string C)
{
----
}
best regards.
6. CPU usage max'd out in ".NET SystemEvents" thread - CSharp/C#
Hi! While working in c# after say 10 minutes , the CPU usage increases to 100% and the framework ide hangs for a couple of minutes. The CPU usage touches hundred as soon as I switch between Design view and code view. I have Win XP SP2. Any solutions?