Microsoft KB Archive/317679

= How to create and make changes to a custom counter for the Windows Performance Monitor by using Visual Basic .NET =

Article ID: 317679

Article Last Modified on 5/10/2004

-

APPLIES TO


 * Microsoft Visual Studio .NET 2003 Professional Edition
 * Microsoft Visual Studio .NET 2003 Enterprise Architect
 * Microsoft Visual Studio .NET 2003 Enterprise Developer
 * Microsoft Visual Studio .NET 2003 Academic Edition
 * Microsoft Visual Studio .NET 2002 Professional Edition
 * Microsoft Visual Studio .NET 2002 Enterprise Architect
 * Microsoft Visual Studio .NET 2002 Enterprise Developer
 * Microsoft Visual Studio .NET 2002 Academic Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft .NET Framework 1.1
 * Microsoft .NET Framework 1.0
 * Microsoft .NET Framework Class Libraries 1.1
 * Microsoft .NET Framework Class Libraries 1.0

-



This article was previously published under Q317679



IN THIS TASK

 * SUMMARY
 * Create a custom counter
 * Complete the code listing (Module1.vb)
 * Verify that the code works
 * Troubleshooting
 * REFERENCES



SUMMARY
This step-by-step article explains how to create a custom performance counter that you can view in the Windows Performance Monitor snap-in.

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that are required:
 * Windows 2000 Professional (or Server) -or-
 * Windows XP Professional with the .NET Framework installed

This article assumes that you are familiar with Windows performance counters.

back to the top

Create a custom counter
To create a custom performance counter that you can view, follow these steps:  Start Visual Studio .NET. On the File menu, point to New and then click Project. Click Visual Basic Projects under Project Types, and then click Console Application under Templates. In the Name box, type HowToPerfCounter, and then click OK. Confirm that the Module1.vb file is open in the Code Editor window, right-click the file, and then click View Code.  At the top of the code, paste the following Imports statements, which allow you to access classes in the associated System.Diagnostics namespace and System.Threading namespace. Imports System.Diagnostics Imports System.Threading   In Sub Main, paste the following code to create an instance of the CounterCreationData class. This class contains information about, or defines, custom counters: ' Create an object that sets the data for a custom counter. Dim CounterData As New CounterCreationData CounterData.CounterName = &quot;ThreadCounter&quot; CounterData.CounterHelp = &quot;some help string&quot; CounterData.CounterType = PerformanceCounterType.NumberOfItems64 </li>  Create an instance of the CounterCreationDataCollection class that holds the new counter data, and then add the CounterCreationData object to the collection: ' Create an instance of the CounterCreationDataCollection class, ' and then add the new custom counter. Dim CounterDatas As New CounterCreationDataCollection CounterDatas.Add(CounterData) </li>  Although you do not have to, it may be worthwhile to add some code to delete any existing counters that have the same name so that you can more easily rerun the program: ' Some code so that you can easily rerun the program. If PerformanceCounterCategory.Exists(&quot;HowToCounter&quot;) Then PerformanceCounterCategory.Delete(&quot;HowToCounter&quot;) End If </li>  To create a performance counter category called HowToCounter and to associate it with the CounterDatas collection object, paste the following code after the If statement that is mentioned in step 9 in the Sub Main procedure. This category will appear in the Performance object drop-down list of the Add Counters dialog box in Windows Performance Monitor (which is explained in &quot;Verify That the Code Works&quot;): ' Create a custom performance counter category. PerformanceCounterCategory.Create(&quot;HowToCounter&quot;, &quot;some category help string&quot;, CounterDatas) </li>  To create a performance counter for the HowToCounter category named ThreadCounter (as set in the CounterData object), add the following code at the end of the code that is mentioned in the previous steps: ' Create an instance of the custom counter that is defined by the CounterCreationData object, above Dim PerfCounter As New PerformanceCounter(&quot;HowToCounter&quot;, &quot;ThreadCounter&quot;, False) </li>  To give yourself time to add the counter to the Windows Performance Monitor snap-in, type the following: ' Pause to give you time to add the counter to the Windows Performance Monitor snap-in. Console.WriteLine(&quot;Counter created. Hit Enter to continue...&quot;) Console.ReadLine </li>  Add the code to simulate the task that you want to performance monitor. The following code iterates through a loop, causes the current thread to sleep for 1 millisecond, and then increments the counter by 5: ' Simulated task for which you want to monitor performance. Dim i As Int32 For i = 0 To 500 Thread.Sleep(1) PerfCounter.IncrementBy(5) Next </li>  Finally, write the resulting counter value to the console, and then add code to pause before you quit the program: ' Write the value to the console. Console.WriteLine(PerfCounter.RawValue)

' Pause and wait to manually exit. Console.WriteLine(&quot;Press ENTER to Exit&quot;) Console.ReadLine </li></ol>

back to the top

Complete the code listing (Module1.vb)
Imports System.Diagnostics Imports System.Threading

Module Module1

Sub Main ' Create an object that sets the data for a custom counter. Dim CounterData As New CounterCreationData CounterData.CounterName = &quot;ThreadCounter&quot; CounterData.CounterHelp = &quot;some help string&quot; CounterData.CounterType = PerformanceCounterType.NumberOfItems64

' Create an instance of the CounterCreationDataCollection class, ' and then add the new custom counter. Dim CounterDatas As New CounterCreationDataCollection CounterDatas.Add(CounterData)

' Some code so that you can easily rerun the program. If PerformanceCounterCategory.Exists(&quot;HowToCounter&quot;) Then PerformanceCounterCategory.Delete(&quot;HowToCounter&quot;) End If

' Create a custom performance counter category. PerformanceCounterCategory.Create(&quot;HowToCounter&quot;, &quot;some category help string&quot;, CounterDatas)

' Create an instance of the custom counter that is defined by the CounterCreationData object, above Dim PerfCounter As New PerformanceCounter(&quot;HowToCounter&quot;, &quot;ThreadCounter&quot;, False)

' Pause to give you time to add the counter to the Windows Performance Monitor snap-in. Console.WriteLine(&quot;Counter created. Hit Enter to continue...&quot;) Console.ReadLine

' Simulated task for which you want to monitor performance. Dim i As Int32 For i = 0 To 500 Thread.Sleep(1) PerfCounter.IncrementBy(5) Next

' Write the value to the console. Console.WriteLine(PerfCounter.RawValue)

' Pause and wait to manually exit. Console.WriteLine(&quot;Press ENTER to Exit&quot;) Console.ReadLine End Sub

End Module back to the top

Verify that the code works
<ol> Press F5 to run the program in debug mode.</li> After Counter created appears, click Start, click Control Panel, click Administrative Tools, and then click Performance.</li> To add counters, click the plus sign (+) button on the toolbar. The Add Counters dialog box appears.</li> In the Performance object drop-down list, click HowToCounter.</li> Make sure that ThreadCounter is selected, click Add, and then click Close.</li> To make sure that the thread counter numeric values appear below the graph, in the chart below the graph, click ThreadCounter. (Alternatively, press CRTL+H.)</li> Switch to the console application, position the application on the screen so that you can see the performance monitor, and then press ENTER to continue.</li> Confirm that you can see the thread counter values increment in the Windows Performance Monitor. After the loop is complete, you receive the following message:

2505 Press ENTER to Exit

</li> <li>Press ENTER to exit the program.</li></ol>

back to the top

Troubleshooting
If you modify the console application, you must close the Windows Performance Monitor, reopen it, and then add the counter again so that your changes are reflected.

back to the top

<div class="references_section">