Monitoring a Performance Counter


Performance counters are a system-level function of Windows. They are used to track usage of system resources. Performance counters can be expressed as counts (number of times a Web page was hit), or percentages (how much disk space is left), or other types of information. Many counters are automatically maintained by the operating system, but applications can create and manage their own performance counters.

To demonstrate how services can interact with system-level functionality, you will add to the CounterMonitor the capability to monitor a particular performance counter, and only beep when the performance counter exceeds a certain value.

Performance counters can be monitored by a user with the Performance Monitor. A variety of performance counters are built into the operating system, providing access to information such as the number of threads currently active on the system, or the number of documents in a print queue. Any of these, and any custom performance counters, can be graphed in the Performance Monitor.

Creating a Performance Counter

This example creates a performance counter named ServiceCounter. Then you will change CounterMonitor to check that counter and only beep when its value is over 5. To test it, you will also create a small Windows Forms application that increments and decrements the counter.

Performance counters are typically accessed in Visual Studio 2005 through the Server Explorer tab. To see the available performance counters, open Server Explorer, which looks like the screen shown in Figure 31-6.

image from book
Figure 31-6

To see the categories of performance counters, click the plus sign next to the Performance Counters option in the Server Explorer. Several dozen categories will be shown. You can look at the counters in any particular category by clicking the plus sign next to the category.

You can also create new categories and new counters. For this example, you need to create a new category for the counter called Service Counters. To do that, right-click the Performance Counters option in the Server Explorer and select the Create New Category option. In the resulting Performance Counter Builder dialog box (shown in Figure 31-7), enter the name of the category as Service Counters, and create a new counter by clicking the New button and entering TestCounter for the name. Once that is complete, click the OK button. Visual Studio 2005 will then create a new category called Service Counters that contains a single performance counter called TestCounter.

image from book
Figure 31-7

Integrating the Counter into the Service

Using a performance counter in the CounterMonitor service you created earlier is straightforward. Open the CounterMonitor project and go to the design surface for CounterMonitor. Then open the Server Explorer so that it shows the TestCounter performance counter you created. Click TestCounter from within the Server Explorer and drag it onto the CounterMonitor.vb design surface.

A new visual control named PerformanceCounter1 will appear on the page’s design surface, ready for use. Change the logic in the Elapsed event for Timer1 as shown here:

  If PerformanceCounter1.RawValue > mnMaxValue Then     Beep() End If 

The RawValue property being used in this code fetches the unformatted value of the counter. For counters that track whole numbers (such as the number of times a Web page is hit), the RawValue property is normally used to get the value of the counter for testing or display. Some other types of counters use a NextValue method to get a formatted value. See the CounterType property of the PerformanceCounter class for more information on the types of performance counters available.

Next, put this statement in the code module just under the first line of the CounterMonitor class:

  Dim mnMaxValue As Integer = 5 

This creates the mnMaxValue as a Private variable. Now build the service again, install it as before, and start the service. It should not beep at this point because the value in the performance counter is zero. You can leave the counter running, because you will now create a program to change the value in the performance counter, thereby making the service begin beeping.

Changing the Value in the Performance Counter

To manipulate the performance counter, we will build a small forms-based application. Close the CounterMonitor solution in Visual Studio and start a new Windows Application Project named CounterTest. Place two buttons on Form1 and change their properties as shown in the following table:

Open table as spreadsheet

Name

Text

BtnIncrement

Increment Counter

BtnDecrement

Decrement Counter

Then, open the Server Explorer and drag the TestCounter performance counter onto the form itself, just as you did earlier with the CounterMonitor project. As with all nonvisible components from the Toolbox, the counter will appear in the component tray (just under the form), rather than on the form’s design surface.

The PerformanceCounter1 control for CounterTest needs one property change. Set the ReadOnly property of the control to False. This enables the application to manipulate the counter. (This change was unnecessary for the CounterMonitor Windows Service project because that project only reads the value of the performance counter and does not change it.)

Now double-click btnIncrement to get to its click event. Place the following code in the event:

  PerformanceCounter1.Increment() 

Double-click the btnDecrement to get to its click event. Place the following code in the event:

  PerformanceCounter1.Decrement() 

Build and run the program and click the Increment button six times. If the CounterMonitor service is running, on the sixth click it will begin beeping because the value in the counter has exceeded five. Then click the Decrement button a couple of times, and the beeping will stop.

If you want to monitor the current value of the counter, select Control Panel image from book Administrative Tools image from book Performance. This program, the Performance Monitor, enables the value of counters to be graphed. To add a counter for display, click the New Counter Set button, right-click the right-hand portion of the Performance Monitor, and add a counter in the dialog box that pops up. Change the Performance Object drop-down list to Service Counters and add the TestCounter performance counter to the list. When completed, press the Close button. The counter that you created will be monitored by the dialog box. You can use the help for this program for more details on displaying counters in the Performance Monitor.




Professional VB 2005 with. NET 3. 0
Professional VB 2005 with .NET 3.0 (Programmer to Programmer)
ISBN: 0470124709
EAN: 2147483647
Year: 2004
Pages: 267

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net