title: Need C# code for showing CPU Utilization in Progress Bar of Windows Form Application
body: <p>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.</p>
  
  <p>Thanks a Lot</p>
  
  <p>Post I have tried:</p>
  
  <blockquote>
    <p><a href="https://stackoverflow.com/questions/278071/how-to-get-the-cpu-usage-in-c">How to get the CPU Usage in C#?</a>
    <a href="https://stackoverflow.com/questions/16331779/need-c-sharp-code-for-showing-cpu-utilization-in-">Need C# code for showing CPU Utilization in Progress Bar of Windows Form Application</a>>progress-bar-of-windows-form-ap</p>
  </blockquote>
  
  <p>I studied about PerformanceCounter and System.Diagnostics but I am unable to code them properly.</p>
  
  <p>Sorry for my 0.1% knowledge in C#.</p>
  
  <p>==============================================================================</p>
  
  <p>My Code:</p>
  
  <pre><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 &gt;= 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";
  
          }
  
  }
  }
  </code></pre>
  
  <p>Now what I'm getting is:</p>
  
  <blockquote>
    <p>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
    &lt;</p>
  </blockquote>