Blue Theme Orange Theme Green Theme Red Theme
 
DevExpress Free UI Controls
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
6 Months Free & No Setup Fees ASP.NET Hosting!
Search :       Advanced Search »
Home » Visual C# » Monitor and Display CPU State Information

Monitor and Display CPU State Information

This article describes a quick and simple approach to displaying information about the state of the CPU or CPUs on a targeted machine.

Author Rank :
Page Views : 28346
Downloads : 1016
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
CpuUsage.zip
 
 
Team Foundation Server Hosting
Become a Sponsor
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

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 Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Scott Lysle
Freelance software developer residing in Alabama. Bachelors, Masters Degrees from Wichita State University. I spent the first half of my career working on aircraft controls and displays and in that time I worked on the cockpits for the OH-58 AHIP, the AH-1W, the V-22, the F-22, the C-130J, the C-5 AMP, AWACS, JPATS, and a few others. Since 1997 I have been largely involved with Windows and web development, GIS application development, consumer electronics development (embedded linux/java), but still sometimes work on aircraft and military projects, the most recent of which was the presidential transport helicopter. I tend to work primarily with C/C++, Java, VB, and C#.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Nevron Chart
Become a Sponsor
 Comments
thank's by randhir On January 14, 2009
Thank's for this code
Reply | Email | Modify 
Re: thank's by Scott On January 20, 2009
I'm glad you found it useful.
Reply | Email | Modify 
Thank you! by N On February 11, 2009
Thank's, really helpful!!
Reply | Email | Modify 
Re: Thank you! by Scott On February 17, 2009
Thanks!
Reply | Email | Modify 
Discover the top 5 tips for understanding .NET Interop
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.