0

Someone please provide code for displaying current CPU Utilization on a Progress Bar of a Windows Form. I have tried with solutions present on this site but getting so many errors. Have no idea what is wrong with the code as I am a newbie in C#. I will pick up from there for rest of my programming because i want to restart a specific windows process after that.

Thanks a Lot

Post I have tried:

How to get the CPU Usage in C#? Need C# code for showing CPU Utilization in Progress Bar of Windows Form Application>progress-bar-of-windows-form-ap

I studied about PerformanceCounter and System.Diagnostics but I am unable to code them properly.

Sorry for my 0.1% knowledge in C#.

==============================================================================

My Code:

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 CPU_Utilization_Monitor   
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

        int totalHits = 0;

        public object getCPUCOunter()
        {

            PerformanceCounter cpuCounter = new PerformanceCounter();
            cpuCounter.CategoryName = "Processor";
            cpuCounter.CounterName = "% Processor Time";
            cpuCounter.InstanceName = "_Total";

                         // will always start at 0
            dynamic firstValue = cpuCounter.NextValue();
            System.Threading.Thread.Sleep(1000);
                        // now matches task manager reading
            dynamic secondValue = cpuCounter.NextValue();

            return secondValue;

        }


        private void Timer1_Tick(System.Object sender, System.EventArgs e)
        {
            int cpuPercent = (int)getCPUCOunter();
            if (cpuPercent >= 90)
            {
                totalHits = totalHits + 1;
                if (totalHits == 60)
                    MessageBox.Show("ALERT 90% usage for 1 minute");
                totalHits = 0;
            }
            else
            {
                totalHits = 0;
            }

            label1.Text = cpuPercent + " % CPU";
            label3.Text = totalHits + " seconds over 20% usage";

        }

}
}

Now what I'm getting is:

Error 1 Program 'C:\Users\SnowFlake\Documents\Visual Studio 2010\Projects\CPU Utilization Monitor\CPU Utilization Monitor\obj\x86\Debug\CPU Utilization Monitor.exe' does not contain a static 'Main' method suitable for an entry point CPU Utilization Monitor <

Community
  • 1
  • 1
ABX
  • 113
  • 1
  • 4
  • 12
  • Post what you tried and what errors you are stuck at. – nvoigt May 02 '13 at 06:34
  • What exact problems do you run into when you try this for example: http://stackoverflow.com/questions/278071/how-to-get-the-cpu-usage-in-c – Corak May 02 '13 at 06:38
  • Would be easier if you'd provide a minimal but complete program of what you are trying. The error states that you basically try to acces *object* members/methods on *class* level (static). Maybe reading up on [Static Classes and Static Class Members](http://msdn.microsoft.com/library/79b3xss3.aspx) might help you. – Corak May 02 '13 at 06:52

1 Answers1

2

This is a complete program that shows how to work with PerformanceCounter taken from https://stackoverflow.com/a/278088/1336590 :

using System;
using System.Diagnostics;

namespace CpuUsage
{
  class Program
  {
    static void Main(string[] args)
    {
      PerformanceCounter cpuCounter = new PerformanceCounter();
      cpuCounter.CategoryName = "Processor";
      cpuCounter.CounterName = "% Processor Time";
      cpuCounter.InstanceName = "_Total";
      PerformanceCounter ramCounter = new PerformanceCounter("Memory", "Available MBytes");

      var unused = cpuCounter.NextValue(); // first call will always return 0
      System.Threading.Thread.Sleep(1000); // wait a second, then try again
      Console.WriteLine("Cpu usage: " + cpuCounter.NextValue() + "%");
      Console.WriteLine("Free ram : " + ramCounter.NextValue() + "MB");

      Console.ReadKey();
    }
  }
}

And as other answers in the refered question indicate, you might need to call cpuCounter.NextValue() twice to get propper readings.

Community
  • 1
  • 1
Corak
  • 2,688
  • 2
  • 21
  • 26