Visual Studio Hacks [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Visual Studio Hacks [Electronic resources] - نسخه متنی

Andrew Lockhart

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید







Hack 73. Access Performance Counters

Windows' performance counters
can reveal all sorts of information about the inner working of your
machine. Use the Server Explorer to interact with these performance
counters.

The Server Explorer provides an excellent
interface to access the performance counters on your computer. Using
the Server Explorer, you can drag performance counters directly from
the Server Explorer to your application. In this hack, you will see
how to build a simple application that reads from a performance
counter and reports its value to the user.

The Server Explorer includes a node called Performance Counters,
which can be seen in Figure 9-1.


Figure 9-1. Server ExplorerPerformance Counters node

Each of the nodes you see in Figure 9-1 is a
category that includes a number of performance counters listed
beneath it. The contents of the
Memory category can be seen in Figure 9-2.


Figure 9-2. Server ExplorerMemory performance counters

From the Server Explorer, you can then drag the performance counter
to your Windows Forms, and you will see the
counter show up in the component section of your form, as seen in
Figure 9-3.


Figure 9-3. Performance counter component


Once
the performance counter object has been added to your form, you can
then access the performance counter through your code. The following
code is a simple example of reading from the Available MBytes
performance counter:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Diagnostics;
namespace ServerExplorerExamples
{
public class PerfCounterExample : System.Windows.Forms.Form
{
private PerformanceCounter performanceCounter1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.ComponentModel.Container components = null;
public PerfCounterExample( )
{
InitializeComponent( );
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose( );
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeComponent( )
{
this.performanceCounter1 = new PerformanceCounter( );
this.label1 = new System.Windows.Forms.Label( );
this.label2 = new System.Windows.Forms.Label( );
((System.ComponentModel.ISupportInitialize)
(this.performanceCounter1)).BeginInit( );
this.SuspendLayout( );
this.performanceCounter1.CategoryName = "Memory";
this.performanceCounter1.CounterName = "Available MBytes";
this.performanceCounter1.MachineName = "JAMESA-TABLET";
this.label1.Location = new System.Drawing.Point(8, 32);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(96, 16);
this.label1.TabIndex = 0;
this.label1.Text = "Available MBytes:";
this.label2.Location = new System.Drawing.Point(112, 32);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(100, 16);
this.label2.TabIndex = 1;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "PerfCounterExample";
this.Text = "PerfCounterExample";
this.Load +=
new System.EventHandler(this.PerfCounterExample_Load);
((System.ComponentModel.ISupportInitialize)
(this.performanceCounter1)).EndInit( );
this.ResumeLayout(false);
}
#endregion
private void PerfCounterExample_Load(
object sender,
System.EventArgs e)
{
//Get the value of the performance counter
label2.Text = performanceCounter1.NextValue( ).ToString( );
}
}
}

When

you run
this code, it will produce the form shown in Figure 9-4.


Figure 9-4. PerfCounter Example form


9.2.1. Counter Information


You can view additional information about
counters by right-clicking on a category and clicking the View
Category menu item. This will display the
Performance Counter Builder form (Figure 9-5), which includes a description of the category
as well as a list of all the performance counters in that category.
Click on a performance counter and you will be shown a description of
it. This is a great way to investigate the counters in each category
and what they report on.


Figure 9-5. Performance Counter Builder screen

The Server Explorer makes it easy to browse all the performance
counters on your machine and then access them in your
application.


/ 172