Monitor and display CPU state information in C #

Source: Internet
Author: User
Tags resource visual studio

Brief introduction

This article describes an easy way to display CPU state information, and demonstrates how to display the percentage of processor time consumed by the CPU by configuring a performance counter and periodically polling the percentage of processor time with a standard progress bar control and a label control.

Figure 1: Show CPU Processor Time

This example can also be used to monitor multiple processors on a single computer, simply by getting the number of CPUs at run time (System.Environment.ProcessorCount can return this value) and dynamically adding performance Monitor controls. Let them monitor a CPU on their own.

You can also extend the methods in this article, such as the following screenshot of a program that uses the Dundas. NET Gauge Control and displays the Intel Centrino Duo processor, although this example is not included in the article, but the idea is the same.

Figure 2: Monitoring each processor in a dual-core system

Start Engineering

Create a new WinForm (Windows Forms application) project in Visual Studio 2005-This form is used to display CPU information, and the solution does not refer to libraries other than the default. Figure 3 is the project in the Solution Resource Explorer:

Figure 3: Solution Resource Explorer

The main form of the program consists of the following standard Toolbox controls:

A group box control

A Label control

A progress bar control

A PerformanceCounter control

A Timer control

The group box control is used to contain other controls, the progress bar control is set to display a value from 0 to 100, the Step property is set to 1, the label control is used to display the current value of the progress bar, and the progress bar represents the percentage of processor time that the PerformanceCounter control captures ; The progress bar and the label control are updated by the Timer control's tick event handler, and the timer is set to invoke the Tick event handler every 10000 milliseconds, and the timer starts when the program initializes.

The properties of the PerformanceCounter control are set to capture the percentage of processor time, either in code settings or in the property bar in the IDE, which in this case is set in the property bar, as shown in the following illustration:

Figure 4:performance Counter Control properties

In the above illustration, the property CategoryName is set to "Processor", the property CounterName is set to "% Processor time" and the InstanceName is set to "_total". If you want to monitor the status of a single processor, attribute instancename must be set to point to a particular processor (for example, 0 or 1).

After you set these properties, you need to write some code, which is the code for the main form of the program:

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() + "%";
}
}
}

It's easy! Just add a little code outside the IDE's default build here, primarily a handler for the Tick event, in which the progress bar is set to display values derived from the performance counter, and the label control is updated by the value passed to the progress bar, both of which are updated each time the tick event is triggered.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.