A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
Using Performance Counters in C#The application we are building in this tutorial will make use of the Performance Counters made available by Windows. They allow us to retrieve CPU, memory and network usage among other counters. |
On Saturday, June 30th 2007 at 12:16 AM By Andrew Pociu (View Profile) ![]() ![]() ![]() ![]() (Rated 4.7 with 31 votes) |
||
|
The performance counters offered by the Windows Operating system can be accessed through Control Panel -> Administrative Tools -> Performance. But what's so great is that these performance counters can be easily queried from inside .NET Framework. First of all, I want to make clear that the application we are building in this tutorial does not work with many of the performance counters that it lists, simply because there's too much code to write to make it adapt to different counter type, and we want to keep things simple. Fortunately it works with the most popular performance characters such as the processor usage and disk usage. Furthermore, since the graph that you see in the picture below is for fixed values of 0 to 100 (typical percentage values) it will not display correctly those performance counters that return values with a different range. Getting a list of available Performance CountersIn order to make this tutorial more useful we're not going to use a fixed, predefined list of counters, instead we're going to retrieve them dinamically from the user's operating system. This is particularly useful because the number of performance counters differ from system to system, depending on the hardware and software installed. Counters are divided into categories, thus first we need to retrieve these categories. We'll do this in the Load event of the form. But first let's place some using statements:using System.Drawing.Drawing2D; using System.Diagnostics; And then initialize a variable we'll use to set the position of the graph line: // Used to move the graph line int graphIncrement = 0; And now the Form1_Load event: private void Form1_Load(object sender, EventArgs e) { // Get a list of available performance counter categories PerformanceCounterCategory[] perfCounters = PerformanceCounterCategory.GetCategories(); for (int i = 0; i < perfCounters.Length; i++) { // Add the category to the drop-down list lstCats.Items.Add(perfCounters[i].CategoryName); } } Now that we have the categories in the lstCats dropdown list, we should retrieve the counters depending on the selected category from the dropdown list. This will be done in the SelectedIndexChanged event of lstCats list, which fires each time a selection is made from the dropdown list: private void lstCats_SelectedIndexChanged(object sender, EventArgs e) { string[] instanceNames; System.Collections.ArrayList counters = new System.Collections.ArrayList(); if (lstCats.SelectedIndex != -1) { System.Diagnostics.PerformanceCounterCategory mycat = new System.Diagnostics.PerformanceCounterCategory(this.lstCats.SelectedItem.ToString()); // Remove the current contents of the list. this.lstCounters.Items.Clear(); // Retrieve the counters. instanceNames = mycat.GetInstanceNames(); if (instanceNames.Length == 0) { counters.AddRange(mycat.GetCounters()); } else { for (int i = 0; i < instanceNames.Length; i++) { counters.AddRange(mycat.GetCounters(instanceNames[i])); } }
// Add the retrieved counters to the list. foreach (System.Diagnostics.PerformanceCounter counter in counters) { this.lstCounters.Items.Add(counter.CounterName); } } } For the code above to work, don't forget to bind lstCats_SelectedIndexChanged to the SelectedIndexChanged event. The easiest way to do this in Visual Studio is to select the dropdown list, switch the Properties window to Events and double click the SelectedIndexChanged field. We're not done yet with retrieving counters. Each counter has its own instances, which could reflect different values to be retrieved by that counter, some in percentages and others in various ranges. Some may not have any instances at all. _Total is a popular instance name which most of the time is used to retrieve the most popularly demanded type of value from that counter. Each time a counter is selected we want to retrieve the list of possible instances: private void lstCounters_SelectedIndexChanged(object sender, EventArgs e) { // Clear the existing instance list lstInstances.Items.Clear(); PerformanceCounterCategory perfCat = new PerformanceCounterCategory(lstCats.SelectedItem.ToString()); string[] catInstances; catInstances = perfCat.GetInstanceNames(); lstInstances.Items.Clear(); foreach (string catInstance in catInstances) { lstInstances.Items.Add(catInstance); } } And now that the code for populating the instance list is in, we can write the code that starts the monitoring, and this is located in the Click even of btnStart: private void btnStart_Click(object sender, EventArgs e) { if (lstCats.SelectedIndex != -1 && lstCounters.SelectedIndex != -1 && lstInstances.SelectedIndex != -1) { // Clear the graph picGraph.Invalidate(); graphIncrement = 0; perfMain.CategoryName = lstCats.SelectedItem.ToString(); perfMain.CounterName = lstCounters.SelectedItem.ToString(); perfMain.InstanceName = lstInstances.SelectedItem.ToString(); tmrCounter.Start(); } else { MessageBox.Show("Please select a category, counter and instance first.", "Selection needed", MessageBoxButtons.OK, MessageBoxIcon.Error); } } Aside from setting the properties for the counter object, we're starting the tmrCounter Timer object - it will be doing all the work from now on, so let's look at what's inside its Tick event: private void tmrCounter_Tick(object sender, EventArgs e) { float currVal; // Move to and get the latest value in the performance counter currVal = perfMain.NextValue(); // Update the label with the value lblValue.Text = Math.Round(Convert.ToDouble(currVal.ToString()), 2) + "%"; Graphics gfx = picGraph.CreateGraphics(); Pen pn = new Pen(Color.Red, 1); gfx.DrawLine(pn, graphIncrement, 200, graphIncrement, 199 - (currVal * 2));
graphIncrement += 2; if (graphIncrement > 500) { picGraph.Invalidate(); graphIncrement = 0; } } Below is the application in action. There are many issues to address, including the PictureBox clearing when it gets invalidated and not all performance counters being properly scaled on the graph, but this is only a proof of concept application. One issue that you might experience though and which can easily be addressed is the "Access to the registry key 'Global' is denied." exception. This is caused on certain operating systems such as Windows Vista Ultimate when the account doesn't have administrative privileges and it's not allowed access to all the performance counters. After you compile the code, right click the application's executable, select Properties and in the Compatibility tab check the "Run this program as an administrator" checkbox.
The full source code for the application is below: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Drawing.Drawing2D; using System.Diagnostics;
namespace PerformanceCounters { public partial class Form1 : Form { // Used to move the graph line int graphIncrement = 0;
public Form1() { InitializeComponent(); }
private void Form1_Load(object sender, EventArgs e) { // Get a list of available performance counter categories PerformanceCounterCategory[] perfCounters = PerformanceCounterCategory.GetCategories(); for (int i = 0; i < perfCounters.Length; i++) { // Add the category to the drop-down list lstCats.Items.Add(perfCounters[i].CategoryName); } }
private void tmrCounter_Tick(object sender, EventArgs e) { float currVal; // Move to and get the latest value in the performance counter currVal = perfMain.NextValue(); // Update the label with the value lblValue.Text = Math.Round(Convert.ToDouble(currVal.ToString()), 2) + "%"; Graphics gfx = picGraph.CreateGraphics(); Pen pn = new Pen(Color.Red, 1); gfx.DrawLine(pn, graphIncrement, 200, graphIncrement, 199 - (currVal * 2));
graphIncrement += 2; if (graphIncrement > 500) { picGraph.Invalidate(); graphIncrement = 0; } }
private void lstCats_SelectedIndexChanged(object sender, EventArgs e) { string[] instanceNames; System.Collections.ArrayList counters = new System.Collections.ArrayList(); if (lstCats.SelectedIndex != -1) { System.Diagnostics.PerformanceCounterCategory mycat = new System.Diagnostics.PerformanceCounterCategory(this.lstCats.SelectedItem.ToString()); // Remove the current contents of the list. this.lstCounters.Items.Clear(); // Retrieve the counters. instanceNames = mycat.GetInstanceNames(); if (instanceNames.Length == 0) { counters.AddRange(mycat.GetCounters()); } else { for (int i = 0; i < instanceNames.Length; i++) { counters.AddRange(mycat.GetCounters(instanceNames[i])); } }
// Add the retrieved counters to the list. foreach (System.Diagnostics.PerformanceCounter counter in counters) { this.lstCounters.Items.Add(counter.CounterName); } } }
private void btnStart_Click(object sender, EventArgs e) { if (lstCats.SelectedIndex != -1 && lstCounters.SelectedIndex != -1 && lstInstances.SelectedIndex != -1) { // Clear the graph picGraph.Invalidate(); graphIncrement = 0; perfMain.CategoryName = lstCats.SelectedItem.ToString(); perfMain.CounterName = lstCounters.SelectedItem.ToString(); perfMain.InstanceName = lstInstances.SelectedItem.ToString(); tmrCounter.Start(); } else { MessageBox.Show("Please select a category, counter and instance first.", "Selection needed", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void lstCounters_SelectedIndexChanged(object sender, EventArgs e) { // Clear the existing instance list lstInstances.Items.Clear(); PerformanceCounterCategory perfCat = new PerformanceCounterCategory(lstCats.SelectedItem.ToString()); string[] catInstances; catInstances = perfCat.GetInstanceNames(); lstInstances.Items.Clear(); foreach (string catInstance in catInstances) { lstInstances.Items.Add(catInstance); } } } } |
|||
Digg It!
Del.icio.us
Reddit
StumbleIt
Newsvine
Furl
BlinkList
|
|||
|
|||
Current CommentsGood code!
Thanks
I HAVE TIRED THSI AMPLE CODE IT NOT GIVING ANY ERROR BUT IT IS NOT SHOWING THE GRAPH.ALSO TELL ME ABOUT WHIC COUTER NAME AND CATAGORY NAME I HAVE TO SET INITIALY.
I HAVE TIRED THSI AMPLE CODE IT NOT GIVING ANY ERROR BUT IT IS NOT SHOWING THE GRAPH.ALSO TELL ME ABOUT WHIC COUTER NAME AND CATAGORY NAME I HAVE TO SET INITIALY.
I HAVE TIRED THSI AMPLE CODE IT NOT GIVING ANY ERROR BUT IT IS NOT SHOWING THE GRAPH.ALSO TELL ME ABOUT WHIC COUTER NAME AND CATAGORY NAME I HAVE TO SET INITIALY.
Its nice to see the code.I am devloping the same counters cpu memory and battery life for windows mobile.Can this code be helpful.Can i get cpu usage from this?
Chakradhar
Its nice to see the code.I am devloping the same counters cpu memory and battery life for windows mobile.Can this code be helpful.Can i get cpu usage from this?
Chakradhar
Good code
it is very nice, but there are some problem with Catgory Name: .NET Network CRL it is not work ,if any one has the soultion for this problem plz submit on sits
Thanks
Good code
it is very nice, but there are some problem with Catgory Name: .NET Network CRL it is not work ,if any one has the soultion for this problem plz submit on sits
Thanks
this code is very good
thank's
That's great !
Thank you for your sharing.
That's great !
Thank you for your sharing.
very good!
I've tried in on my webservice in
Could you please send me the entire solution? I have tried your code, but it doesn't work.
Thanks.
Could you please send me the entire solution? I have tried your code, but it doesn't work.
Thanks.
I Would be using this for KPI Solution Program...where I need to monitor progress of KPI's of personnel...will it work with the graph that was shown in this sample template?
Using C#.NET 2005 on XP -OR-
Using C#.NET (Visual Studio 2010 Ultimate)
Good stuff.
Good stuff.
As a part of the application which m making..i want to calculate the total data transfer between particular interval of time..ie the total amont of download n uploaded data..by the user..cn any1 provide me with d code to calculate the total data transfer in asp.net for web application
could u plz send the code of entire solution ?
this code doesn't give any idea about form design. pls mention it.
please help me for source code on c# for the network monitoring sytsem
im student on the last years
thanks
i need more explanation on remember me checkbox using cookies and Wonderful! I am such a big fan of the blue ridge mountains. There is nothing better then this beauty.and also i want to known that how to assign cookies in one page and get cookies from Another page, but those things r done only in JAVASCRIPT.. [ASP.NET] Help Me...
Your personal know-how and kindness in maneuvering every part was tremendous. I am not sure what I would've done if I hadn't encountered such a solution like this. I'm able to at this time look ahead to my future. Thanks a lot so much for the specialized and result oriented help. I won't hesitate to endorse the blog to anybody who desires assistance on this topic.
Your personal know-how and kindness in maneuvering every part was tremendous. I am not sure what I would've done if I hadn't encountered such a solution like this. I'm able to at this time look ahead to my future. Thanks a lot so much for the specialized and result oriented help. I won't hesitate to endorse the blog to anybody who desires assistance on this topic.
Your personal know-how and kindness in maneuvering every part was tremendous. I am not sure what I would've done if I hadn't encountered such a solution like this. I'm able to at this time look ahead to my future. Thanks a lot so much for the specialized and result oriented help. I won't hesitate to endorse the blog to anybody who desires assistance on this topic.
I truly wanted to write a quick message to thank you for some of the splendid points you are showing at this website. My long internet investigation has at the end been compensated with sensible facts and techniques to share with my family and friends.
I truly wanted to write a quick message to thank you for some of the splendid points you are showing at this website. My long internet investigation has at the end been compensated with sensible facts and techniques to share with my family and friends.
I would believe that we visitors are truly blessed to dwell in a wonderful network with many wonderful professionals with very beneficial hints. I feel extremely privileged to have seen the web pages and look forward to many more pleasurable times reading here. Thank you once again for everything.
I would believe that we visitors are truly blessed to dwell in a wonderful network with many wonderful professionals with very beneficial hints. I feel extremely privileged to have seen the web pages and look forward to many more pleasurable times reading here. Thank you once again for everything.
Thanks so much for providing individuals with an extremely remarkable possiblity to read critical reviews from this web site. It is always so beneficial plus jam-packed with a great time for me personally and my office friends to search the blog at the least three times in one week to see the fresh secrets you will have.
Thanks so much for providing individuals with an extremely remarkable possiblity to read critical reviews from this web site. It is always so beneficial plus jam-packed with a great time for me personally and my office friends to search the blog at the least three times in one week to see the fresh secrets you will have.
I am just always happy with your good tips and hints served by you. Some 1 areas in this article are undeniably the finest I've had.
I am just always happy with your good tips and hints served by you. Some 1 areas in this article are undeniably the finest I've had.
I am just always happy with your good tips and hints served by you. Some 1 areas in this article are undeniably the finest I've had.
Thanks a lot for providing individuals with remarkably special possiblity to read critical reviews from this web site. It can be very kind plus packed with a great time for me personally and my office mates to visit your site no less than three times weekly to study the newest tips you have.
Thanks a lot for providing individuals with remarkably special possiblity to read critical reviews from this web site. It can be very kind plus packed with a great time for me personally and my office mates to visit your site no less than three times weekly to study the newest tips you have.
Once you have recreated the problem and captured these steps, you can save them to a file and send it to your support person, who can then open it up and view
Hey very cool website!! Guy .. Excellent .. Wonderful .. I will bookmark your website and take the feeds additionally¡KI am glad to seek out a lot of helpful information right here in the put up, we need develop more strategies on this regard, thanks for sharing. . . . . .
hey dude it cool
Much appreciated for the information and share!
Hi there,
How would that work if I want to monitor the performance of a remote server?
Hi there,
How would that work if I want to monitor the performance of a remote server?
information right here in the put up, we need develop more strategies on this regard, thanks for sharing. .
the put up, we need develop more strategies on this regard, thanks for sharing. .
Related Tutorials
Related Source Code
C# Job SearchFrom the creators of Geekpedia, a revolutionary new coupon website!
BargainEZ has coupons codes, printable coupons, bargains and it is the leading source of Passbook coupons for iPhone and iPod touch devices.