Monitoring Process CPU usage using WMI
-
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
-
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
I've found that when using WMI you need to use NextValue() as opposed to Value. Try that and see if it works.
-
I've found that when using WMI you need to use NextValue() as opposed to Value. Try that and see if it works.
Hi, Thanks for your reply. I'm looking for NextValue() and I can't find it(not in PropertyData and not in ManagementObject)... :sigh: Thanks again, With best regards, Eli