How to create and make changes to a custom counter for the Windows Performance Monitor by using Visual Basic .NET (317679)
The information in this article applies to:
- Microsoft Visual Studio .NET (2003), Professional Edition
- Microsoft Visual Studio .NET (2003), Enterprise Architect Edition
- Microsoft Visual Studio .NET (2003), Enterprise Developer Edition
- Microsoft Visual Studio .NET (2003), Academic Edition
- Microsoft Visual Studio .NET (2002), Professional Edition
- Microsoft Visual Studio .NET (2002), Enterprise Architect Edition
- Microsoft Visual Studio .NET (2002), Enterprise Developer Edition
- Microsoft Visual Studio .NET (2002), Academic Edition
- Microsoft Visual Basic .NET (2003)
- Microsoft Visual Basic .NET (2002)
- 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 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 topCreate 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 = "ThreadCounter"
CounterData.CounterHelp = "some help string"
CounterData.CounterType = PerformanceCounterType.NumberOfItems64 - 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) - 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("HowToCounter") Then
PerformanceCounterCategory.Delete("HowToCounter")
End If
- 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
"Verify That the Code Works"):
' Create a custom performance counter category.
PerformanceCounterCategory.Create("HowToCounter", "some category help string", CounterDatas) - 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("HowToCounter", "ThreadCounter", False) - 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("Counter created. Hit Enter to continue...")
Console.ReadLine() - 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 - 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("Press ENTER to Exit")
Console.ReadLine() back to the topComplete 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 = "ThreadCounter"
CounterData.CounterHelp = "some help string"
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("HowToCounter") Then
PerformanceCounterCategory.Delete("HowToCounter")
End If
' Create a custom performance counter category.
PerformanceCounterCategory.Create("HowToCounter", "some category help string", CounterDatas)
' Create an instance of the custom counter that is defined by the CounterCreationData object, above
Dim PerfCounter As New PerformanceCounter("HowToCounter", "ThreadCounter", False)
' Pause to give you time to add the counter to the Windows Performance Monitor snap-in.
Console.WriteLine("Counter created. Hit Enter to continue...")
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("Press ENTER to Exit")
Console.ReadLine()
End Sub
End Module back to the topVerify that the code works- Press F5 to run the program in debug mode.
- After Counter created appears, click Start, click Control Panel, click Administrative Tools, and then click Performance.
- To add counters, click the plus sign (+) button on the toolbar. The Add Counters dialog box appears.
- In the Performance object drop-down list, click HowToCounter.
- Make sure that ThreadCounter is selected, click Add, and then click Close.
- 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.)
- 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.
- 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 - Press ENTER to exit the program.
back to the topTroubleshooting 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 topREFERENCES For more information, see the following online Help
documentation: back to the top
Modification Type: | Major | Last Reviewed: | 5/10/2004 |
---|
Keywords: | kbHOWTOmaster kbKernBase kbPerfMon KB317679 kbAudDeveloper |
---|
|