locked
How can i get the CPU Usage of the pc and of specific process? RRS feed

  • Question

  • I want to get the cpu usage of the pc overall usage currently each time lets say every 5 seconds to get updated value of the cpu usage.

    And every 5 second to get the value of specific process name cpu usage. For example i will choose in runtime what process to get the cpu usage of.

    So in the Form1 when im running the program i will see two labels that are updating every 5 seconds.

    This is my code now:

    using System; 
    using
    System.Collections.Generic; 
    using
    System.ComponentModel; 
    using
    System.Data; 
    using
    System.Drawing; 
    using
    System.Linq; 
    using
    System.Text; 
    using
    System.Windows.Forms; 
    using
    System.Diagnostics; 
     
     
    namespace
    CpuUsage 
    { 
       
    public partial class Form1 : Form 
       
    { 
     
           
    private PerformanceCounter theCPUCounter; 
           
    private PerformanceCounter theMemCounter; 
           
    private PerformanceCounter specProcessCPUCounter; 
           
    private float cpuUsage; 
           
    private float memUsage; 
           
    private string processname; 
     
           
    public Form1() 
           
    { 
               
    InitializeComponent(); 
     
     
                    theCPUCounter
    = new PerformanceCounter("Processor", "% Processor Time", "_Total"); 
                    theMemCounter
    = new PerformanceCounter("Memory", "Available MBytes"); 
                    specProcessCPUCounter
    = new PerformanceCounter("Process", "% Processor Time", Process.GetCurrentProcess().ProcessName); 
                    processname
    = specProcessCPUCounter.CounterName; 
                    cpuUsage
    = this.theCPUCounter.NextValue(); 
                    memUsage
    = theMemCounter.NextValue(); 
                    label1
    .Text = memUsage.ToString(); 
     
     
           
    } 
     
           
    private void Form1_Load(object sender, EventArgs e) 
           
    { 
     
           
    } 
       
    } 
    } 

    I have label1 and label2 in designer. The value theCPUCounter is 0 all the time. And i dont know how ot use the specProcessCPUCounter to get the specific process cpuUsage. And i dont know how to make that they will get update every 5 seconds.


    danieli

    Tuesday, August 28, 2012 6:48 PM

Answers

  • I have this variable: specProcessCPUCounter but im still not get it how ot use it to show the specific process pcUsage and memoryUsage.

    You use it the same way you use the other two counters. What is the problem?

    • Marked as answer by chocolade Wednesday, August 29, 2012 7:18 AM
    Wednesday, August 29, 2012 7:17 AM

All replies

  • So i added a timer so the code is now like this:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Diagnostics;
    namespace CpuUsage
    {
        public partial class Form1 : Form
        {
            private PerformanceCounter theCPUCounter;
            private PerformanceCounter theMemCounter;
            private PerformanceCounter specProcessCPUCounter;
            private float cpuUsage;
            private float memUsage;
            private string processname;
            public Form1()
            {
                InitializeComponent();
               
                    theCPUCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
                    theMemCounter = new PerformanceCounter("Memory", "Available MBytes");
                    specProcessCPUCounter = new PerformanceCounter("Process", "% Processor Time", Process.GetCurrentProcess().ProcessName);
                    //label2.Text =  processname = specProcessCPUCounter.CounterName;
                    cpuUsage = this.theCPUCounter.NextValue();
                    
                
            }
            private void Form1_Load(object sender, EventArgs e)
            {
            }
            private void timer1_Tick(object sender, EventArgs e)
            {
                memUsage = theMemCounter.NextValue();
                label1.Text = memUsage.ToString();
                cpuUsage = this.theCPUCounter.NextValue();
                label2.Text = cpuUsage.ToString();
            }
        }
    }

    The problem is how do i get the cpu usage for a specific process ?

    For example if i run the game bfbc2.exe ( battlefield bad company 2 ) and i want to get its cpuUsage how can i do it ?

    I have this variable: specProcessCPUCounter but im still not get it how ot use it to show the specific process pcUsage and memoryUsage.


    danieli

    Tuesday, August 28, 2012 6:53 PM
  • I have this variable: specProcessCPUCounter but im still not get it how ot use it to show the specific process pcUsage and memoryUsage.

    You use it the same way you use the other two counters. What is the problem?

    • Marked as answer by chocolade Wednesday, August 29, 2012 7:18 AM
    Wednesday, August 29, 2012 7:17 AM