Friday, September 16, 2005

Performance Counters in VB.net example

Imports System.Diagnostics
Imports System.Threading
Module Module1
    Dim myCounter As PerformanceCounter
    Dim firstSample As CounterSample

    Sub Main()
        myCounter = New PerformanceCounter
        Try

            myCounter.CategoryName = "Processor"
            myCounter.CounterName = "Interrupts/sec"
            myCounter.InstanceName = "_Total"

            ' Prime the pump - meaningless on first call
            myCounter.NextValue()
            firstSample = myCounter.NextSample

            Dim myTimer As New System.Threading.Timer(AddressOf PerfCountTimerCallback, Nothing, 1000, 1000)
            Try
                Console.WriteLine("VB Performance counter monitoring...")
                Console.WriteLine("Press enter to quit")
                Console.ReadLine()
                ' stop timer
                myTimer.Dispose()

            Catch ex As Exception

            End Try
        Catch ex As Exception
        Finally
            myCounter.Close()
        End Try

    End Sub
    Sub PerfCountTimerCallback(ByVal state As Object)
        Dim nv As Single = myCounter.NextValue
        Dim samp2 As CounterSample = myCounter.NextSample
        Dim avg As Single = CounterSample.Calculate(firstSample, samp2)
        Console.WriteLine("Interrupts - Next: {0} , Average: {1}", nv, avg)

    End Sub
End Module

No comments:

Post a Comment