المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Monitoring Process CPU usage using WMI



C# Programming
03-30-2009, 01:02 PM
Hi,

I'm trying to write an application which monitor another application using WMI.
One of the parameter I need to monitor is CPU usage by the monitored application.
Just for the test - The tested process is devenv.exe(i.e Visual studio).
When running the application and printing to the outout window I see that the process CPU usage is 0%(while the task manager shows 20-30%).
Note that other parameters are OK(for example Virtual memory , Thread count etc.).
This is my code to get all parameters related to devenv.exe:

private void MonitoringTimer_Tick(object sender, EventArgs e)
{
ObjectQuery winQuery = new ObjectQuery("SELECT * FROM Win32_PerfFormattedData_PerfProc_Process WHERE Name = 'devenv'");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(winQuery);
foreach (ManagementObject item in searcher.Get())
{
string s = "";
foreach (PropertyData PC in item.Properties)
{
if (PC.Value != null)
{
s = PC.Name + " : " + PC.Value.ToString();
}
else
{
s = PC.Name + " : NULL";
}
}
s = "******************************************************";
}
}

Can anyone help me solve this problem?
With best regards,
Eli