Reader Level:
ARTICLE

Monitor and Display CPU State Information

Posted by Scott Lysle Articles | Visual C# December 18, 2006
This article describes a quick and simple approach to displaying information about the state of the CPU or CPUs on a targeted machine.
  • 0
  • 0
  • 42860
Download Files:
 

Introduction:

This article describes a quick and simple approach to displaying information about the state of the CPU or CPUs on a targeted machine; the example provided shows how to display the percentage of processor time consumed by the CPU or CPUs on the target machine.This is accomplished by configuring a performance counter and periodically polling for and displaying the percent processor time value using a standard progress bar and label control. This example is representative of only a small portion of the items that may be monitored or captured using the performance counter. The performance counter may also be used to capture a variety of information ranging from the state of the current CLR to the state of the print queue.

Figure 1. Displaying CPU Processor Time

The example shown herein could also be used to monitor the state of multiple processors on a single machine; this could accomplished merely by getting the CPU count (System.Environment.ProcessorCount returns this value) and dynamically adding and populating the Performance Monitor controls at runtime; assigning each to monitor a separate CPU.

Whilst not included in this demo, using the same approach indicated in the last paragraph along with the Dundas .NET Gauge Control, I have set up a form that displays the percent of processor time on a machine equipped with an Intel Centrino Duo dual core processor; an screen shot of this form in use follows:

Figure 2. Monitoring Each Processor in a Dual Core System

Getting Started:

In order to get started, start up the Visual Studio 2005 IDE and open the included project. The solution consists of a Win Forms project with a single form included. The form is used as to display the total CPU information. 

The solution does not use any additional references aside from the defaults.

Figure 3 shows the solution explorer for this project:

Figure 3. Solution Explorer

The Code: Main Form

The main form of the application is populated with the following standard toolbox controls:

  • A Group Box Control
  • A Label Control
  • A Progress Bar Control
  • A Performance Counter Control
  • A Timer Control

The group box control is used to contain the other controls. The progress bar control is set to display a value between 0 and 100 and its step property is set to 1. The label control will be used to display the current numeric value of the progress bar; the progress bar will display processor time percentage as captured from the Performance Counter control. Both the progress bar and the label control will be updated in response to the timer control's tick event handler. The timer control is set to evoke the tick event handler every 1000 msec and it is enabled from initialization.

The properties for the Performance Counter Control are set to capture the processor time percentage and may be set in code or in the IDE's property grid; in this example the values were set in the grid:

Figure 4. Performance Counter Control Properties

To configure the control for the purposes of this example, the Category Name property was set to "Processor", the Counter Name property was set to "% Processor Time", and the Instance Name was set to "_Total". To monitor the state of a single process, the Instance Name property would be set to point to a specific processor (e.g., 0 or 1). You may wish to examine some of the other Category Names and Counter Names available in the drop downs to get a feel for some of the many other things you can examine through this control.

With these properties set, there is little else to do other than write a couple of lines of code; the following is the sum total of all of the code in the main form of the application:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

 

namespace CpuUsageTest

{

    public partial class frmCpuUsage : Form

    {

        public frmCpuUsage()

        {

            InitializeComponent();

        }

 

        private void Form1_Load(object sender, EventArgs e)

        {

 

        }

 

        private void timer1_Tick(object sender, EventArgs e)

        {

            progressBar1.Value = (int)(performanceCounter1.NextValue());

            label1.Text = "Processor Time: " + progressBar1.Value.ToString() + "%";

        }

    }
}

As you can see, the only real addition to the default code is the handler for the timer tick event; in the two lines of code added, the progress bar value is set to display the value from the performance counter. The label control is updated with the value passed the progress bar. These values are updated each time the tick event is fired.

Summary

This article described an approach monitoring the percent of processor time associated with all of the processors on a targeted machine; this could be modified with little difficulty to display the values associated with each of the processors on a multi-processor machine. Aside from the example shown herein, the Performance Counter control may be deployed to monitor or capture a large variety of information about the system as the processors.

COMMENT USING

Trending up