Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Monitoring Process CPU usage using WMI

Monitoring Process CPU usage using WMI

Scheduled Pinned Locked Moved C#
helpcsharpvisual-studioperformancetutorial
3 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    eli15021979
    wrote on last edited by
    #1

    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

    M 1 Reply Last reply
    0
    • E eli15021979

      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

      M Offline
      M Offline
      Michael Bookatz
      wrote on last edited by
      #2

      I've found that when using WMI you need to use NextValue() as opposed to Value. Try that and see if it works.

      E 1 Reply Last reply
      0
      • M Michael Bookatz

        I've found that when using WMI you need to use NextValue() as opposed to Value. Try that and see if it works.

        E Offline
        E Offline
        eli15021979
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups