Results 1 to 6 of 6

Thread: [RESOLVED] displaying cpu usage

  1. #1

    Thread Starter
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    Resolved [RESOLVED] displaying cpu usage

    hi. I'm trying to display the cpu usage ofmycomputer. I'm not a coding genius and i'm sure its just a simple mistake. Here's the code the I have:

    PerformanceCounter CpuCounter;

    CpuCounter = new PerformanceCounter();
    CpuCounter.CategoryName = "Processor";
    CpuCounter.CounterName = "% Processor Time";
    CpuCounter.InstanceName = "_Total";
    txtCPU.Text = CpuCounter.NextValue().ToString();

    Just that its displaying the value 0 every time I run it. Also I tried using the RawValue method but the value being displayed is just too large. Could somebody help me pleaseeeeeee

    Jennifer

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: displaying cpu usage

    Welcome to the forums.

    Take a look at this

  3. #3

    Thread Starter
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    Re: displaying cpu usage

    I got my code from that page. The ram counter is working fine. But the cpu usage is not. It is displaying only 0 every time I run it. I just cannot figure out the problem. Could somebody help me please.

    Jennifer

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: displaying cpu usage

    It actually works fine, but the thing is it returns the average CPU usage between calls to NextValue(). The easiest way to get an approximation of average CPU usage is to set up a timer and query the CPU usage every 200 ms or so. The actual value that is returned will depend on your timer interval, but 200 ms gives you a nice balance between accuracy and update time.

    Drop this in to test it out.

    Code:
    using System.Diagnostics;
    
    public partial class Form1 : Form
    {
        private Timer updateTimer;
        private PerformanceCounter cpuCounter;
    
        public Form1 ()
        {
            InitializeComponent();
    
            updateTimer = new Timer();
            updateTimer.Interval = 200;
            updateTimer.Tick += new EventHandler(updateTimer_Tick);
    
            InitialiseCounter();
    
            updateTimer.Start();
        }
    
        private void InitialiseCounter ()
        {
            cpuCounter = new PerformanceCounter(
                "Processor", 
                "% Processor Time", 
                "_Total", 
                true
            );
        }
    
        private void updateTimer_Tick (object sender, EventArgs e)
        {
            this.Text = "CPU Usage: " + 
                        Convert.ToInt32(cpuCounter.NextValue()).ToString() +
                        "%";
        }
    }

  5. #5

    Thread Starter
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    Re: displaying cpu usage

    Thanks a million penegate! I would give you a kiss if i could right now. It's working fine. Thanks so much for the help, I really appreciate it. Thank you for typing out the code so I could understand.

    Jennifer

  6. #6
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: displaying cpu usage

    Heheh, any time

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width