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. Getting CPU Usage

Getting CPU Usage

Scheduled Pinned Locked Moved C#
asp-netquestionannouncement
3 Posts 2 Posters 0 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.
  • J Offline
    J Offline
    Johan Martensson
    wrote on last edited by
    #1

    I'm trying to get the cpu usage of the running processes and I thought I had found the answer the PerformanceCounter but I can't really get it to work. I start of by filling a ListView with the running processes and then update mem-usage and cpu-usage every second with a Timer but the cpu-usage is always reported to be 0. Could this has something to do with that I'm using a dual-core processor or is something else wrong? This is what I've done...

    public void GetProcesses()
    {
    pThread = new Thread(FillProcessList);
    pThread.Start();
    }

    private void FillProcessList()
    {
    Process[] myProcesses = Process.GetProcesses();

    for (int i = 0; i < myProcesses.Length; i++)
    {
        int pId = myProcesses\[i\].Id;
        string pName = myProcesses\[i\].ProcessName;
        string pUser = GetProcessOwner(pId);
    
        ListViewItem item = new ListViewItem(new string\[\] { pId.ToString(), pName, pUser, string.Empty, string.Empty });
        item.Tag = myProcesses\[i\];
        lvProcesses.Items.Add(item);
    }
    

    }

    private void TrackProcesses()
    {
    if (pThread != null)
    if (!pThread.IsAlive)
    {
    updateTimer.Stop();
    foreach (ListViewItem item in lvProcesses.Items)
    {
    Process p = Process.GetProcessById(((Process)item.Tag).Id);

                item.SubItems\[3\].Text = (p.WorkingSet64 / 1024.0M).ToString();
    
                PerformanceCounter counter = new PerformanceCounter();
                counter.CategoryName = "Process";
                counter.CounterName = "% Processor Time";
                counter.InstanceName = p.ProcessName;
    
                item.SubItems\[4\].Text = counter.NextValue().ToString();
    
            }
            updateTimer.Start();
        }
    

    }

    public void StopPThread()
    {
    if (pThread != null)
    pThread.Abort();
    }

    private void updateTimer_Tick(object sender, EventArgs e)
    {
    TrackProcesses();
    }

    http://johanmartensson.se - Home of MPEG4Watcher

    S 1 Reply Last reply
    0
    • J Johan Martensson

      I'm trying to get the cpu usage of the running processes and I thought I had found the answer the PerformanceCounter but I can't really get it to work. I start of by filling a ListView with the running processes and then update mem-usage and cpu-usage every second with a Timer but the cpu-usage is always reported to be 0. Could this has something to do with that I'm using a dual-core processor or is something else wrong? This is what I've done...

      public void GetProcesses()
      {
      pThread = new Thread(FillProcessList);
      pThread.Start();
      }

      private void FillProcessList()
      {
      Process[] myProcesses = Process.GetProcesses();

      for (int i = 0; i < myProcesses.Length; i++)
      {
          int pId = myProcesses\[i\].Id;
          string pName = myProcesses\[i\].ProcessName;
          string pUser = GetProcessOwner(pId);
      
          ListViewItem item = new ListViewItem(new string\[\] { pId.ToString(), pName, pUser, string.Empty, string.Empty });
          item.Tag = myProcesses\[i\];
          lvProcesses.Items.Add(item);
      }
      

      }

      private void TrackProcesses()
      {
      if (pThread != null)
      if (!pThread.IsAlive)
      {
      updateTimer.Stop();
      foreach (ListViewItem item in lvProcesses.Items)
      {
      Process p = Process.GetProcessById(((Process)item.Tag).Id);

                  item.SubItems\[3\].Text = (p.WorkingSet64 / 1024.0M).ToString();
      
                  PerformanceCounter counter = new PerformanceCounter();
                  counter.CategoryName = "Process";
                  counter.CounterName = "% Processor Time";
                  counter.InstanceName = p.ProcessName;
      
                  item.SubItems\[4\].Text = counter.NextValue().ToString();
      
              }
              updateTimer.Start();
          }
      

      }

      public void StopPThread()
      {
      if (pThread != null)
      pThread.Abort();
      }

      private void updateTimer_Tick(object sender, EventArgs e)
      {
      TrackProcesses();
      }

      http://johanmartensson.se - Home of MPEG4Watcher

      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #2

      The MSDN[^] documentation for NextValue says that invoking the property on a newly created PerformanceCounter instance is like resetting the counter - you'll have to call it twice, with a (recommended) interval of one second. Alternatively, you could make the counter a member variable of the class and call NextValue once in the constructor. This way, it gets created only once and successive calls will return the correct values.

      Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro

      J 1 Reply Last reply
      0
      • S S Senthil Kumar

        The MSDN[^] documentation for NextValue says that invoking the property on a newly created PerformanceCounter instance is like resetting the counter - you'll have to call it twice, with a (recommended) interval of one second. Alternatively, you could make the counter a member variable of the class and call NextValue once in the constructor. This way, it gets created only once and successive calls will return the correct values.

        Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro

        J Offline
        J Offline
        Johan Martensson
        wrote on last edited by
        #3

        Thanks, I'll look into that

        http://johanmartensson.se - Home of MPEG4Watcher

        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