Connecting Tech Pros Worldwide Help | Site Map

PerformanceCounter CPU 0% ???

Newbie
 
Join Date: Feb 2008
Posts: 13
#1: Jan 27 '09
Hi there!

Currently I'm trying to display the current performance of my servers on my website. I tried it with the following code:

Expand|Select|Wrap|Line Numbers
  1.     protected static PerformanceCounter cpuCounter;
  2.     protected static PerformanceCounter ramCounter;
  3.  
  4.     protected void Page_Load(object sender, EventArgs e)
  5.     {
  6.         try
  7.         {
  8.             Timer1.Tick += new EventHandler<EventArgs>(Timer1_Tick);
  9.             Timer1.Interval = 2000;
  10.  
  11.             cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
  12.             ramCounter = new PerformanceCounter("Memory", "Available MBytes");
  13.         }
  14.         catch (Exception exc)
  15.         { 
  16.         lbl_error.Text = "Error: " + exc.Message;
  17.         }
  18.     }
  19.  
  20.     void Timer1_Tick(object sender, EventArgs e)
  21.     {
  22.         lbl_text.Text = "<b>WebServer</b> <br/>";
  23.         lbl_text.Text += "CPU Usage: " +getCurrentCpuUsage() + "<br/>";
  24.         lbl_text.Text += "Available Ram: " + getAvailableRAM() + "<br/>";
  25.     }
  26.  
  27.     public string getCurrentCpuUsage()
  28.     {
  29.         return cpuCounter.NextValue() + "%";
  30.     }
  31.     public string getAvailableRAM()
  32.     {
  33.             return ramCounter.NextValue()+"Mb";
  34.     }
  35.  
Currently only the available ram is being showed on the page and the CPU load stays on 0% while in the Performance monitor of Microsoft it says otherwise :S

Thanks in advanced!
insertAlias's Avatar
Forum Leader
 
Join Date: Apr 2008
Location: San Antonio, TX (USA)
Posts: 2,603
#2: Jan 27 '09

re: PerformanceCounter CPU 0% ???


I have the same results as you. I'm interested in this thread, hope someone has an answer.
Reply