Blue Theme Orange Theme Green Theme Red Theme
 
MindFusion's Components
Home | Forums | Videos | Photos | Downloads | Blogs | E-Books | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » Visual Studio 2010 » 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:
Technologies: .NET 1.0/1.1, Common Controls, Windows Forms,Visual C# .NET
Total downloads : 671
Total page views :  17473
Rating :
 4.67/5
This article has been rated :  3 times
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
CpuUsage.zip
 
Become a Sponsor


Related EbooksTop Videos

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.


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.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010
Microsoft Visual Studio 2010 offers more to developers than any other Visual Studio release. Work more productively and collaboratively-with greater control over your work at every step. The Beta 2 can give you a head start on achieving efficiency.
 
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
CpuUsage.zip
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
Dundas Dashboard
Become a Sponsor
 Comments
thank's by randhir On January 14, 2009
Thank's for this code
Reply | Email | Delete | Modify | 
Re: thank's by Scott On January 20, 2009
I'm glad you found it useful.
Reply | Email | Delete | Modify | 
Thank you! by N On February 11, 2009
Thank's, really helpful!!
Reply | Email | Delete | Modify | 
Re: Thank you! by Scott On February 17, 2009
Thanks!
Reply | Email | Delete | Modify | 

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 1999 - 2009  Mindcracker LLC. All Rights Reserved