Performance Monitor : Chart « GUI Applications « VB.Net Tutorial






Imports System.Collections
Imports System.Diagnostics

Imports System.Windows.Forms

public class PerformanceMonitor
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents ObjectListBox As System.Windows.Forms.ListBox
    Friend WithEvents CounterListBox As System.Windows.Forms.ListBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents InstanceListBox As System.Windows.Forms.ListBox
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents CounterValueTextBox As System.Windows.Forms.TextBox
    Friend WithEvents CounterValueLabel As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.ObjectListBox = New System.Windows.Forms.ListBox()
        Me.CounterListBox = New System.Windows.Forms.ListBox()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.InstanceListBox = New System.Windows.Forms.ListBox()
        Me.Label3 = New System.Windows.Forms.Label()
        Me.CounterValueTextBox = New System.Windows.Forms.TextBox()
        Me.CounterValueLabel = New System.Windows.Forms.Label()
        Me.SuspendLayout()
        '
        'ObjectListBox
        '
        Me.ObjectListBox.Location = New System.Drawing.Point(16, 48)
        Me.ObjectListBox.Name = "ObjectListBox"
        Me.ObjectListBox.Size = New System.Drawing.Size(176, 134)
        Me.ObjectListBox.Sorted = True
        Me.ObjectListBox.TabIndex = 0
        '
        'CounterListBox
        '
        Me.CounterListBox.Location = New System.Drawing.Point(16, 208)
        Me.CounterListBox.Name = "CounterListBox"
        Me.CounterListBox.Size = New System.Drawing.Size(424, 134)
        Me.CounterListBox.TabIndex = 1
        '
        'Label1
        '
        Me.Label1.AutoSize = True
        Me.Label1.Location = New System.Drawing.Point(16, 32)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(111, 13)
        Me.Label1.TabIndex = 2
        Me.Label1.Text = "Performance Objects"
        '
        'Label2
        '
        Me.Label2.AutoSize = True
        Me.Label2.Location = New System.Drawing.Point(16, 192)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(118, 13)
        Me.Label2.TabIndex = 3
        Me.Label2.Text = "Performance Counters"
        '
        'InstanceListBox
        '
        Me.InstanceListBox.Location = New System.Drawing.Point(240, 48)
        Me.InstanceListBox.Name = "InstanceListBox"
        Me.InstanceListBox.Size = New System.Drawing.Size(200, 134)
        Me.InstanceListBox.TabIndex = 4
        '
        'Label3
        '
        Me.Label3.AutoSize = True
        Me.Label3.Location = New System.Drawing.Point(240, 32)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(88, 13)
        Me.Label3.TabIndex = 5
        Me.Label3.Text = "Instance Objects"
        '
        'CounterValueTextBox
        '
        Me.CounterValueTextBox.Location = New System.Drawing.Point(128, 360)
        Me.CounterValueTextBox.Name = "CounterValueTextBox"
        Me.CounterValueTextBox.Size = New System.Drawing.Size(312, 20)
        Me.CounterValueTextBox.TabIndex = 6
        Me.CounterValueTextBox.Text = ""
        Me.CounterValueTextBox.TextAlign = System.Windows.Forms.HorizontalAlignment.Right
        '
        'CounterValueLabel
        '
        Me.CounterValueLabel.AutoSize = True
        Me.CounterValueLabel.Location = New System.Drawing.Point(16, 360)
        Me.CounterValueLabel.Name = "CounterValueLabel"
        Me.CounterValueLabel.Size = New System.Drawing.Size(62, 13)
        Me.CounterValueLabel.TabIndex = 7
        Me.CounterValueLabel.Text = "Raw Value:"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(456, 469)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.CounterValueLabel, Me.CounterValueTextBox, Me.Label3, Me.InstanceListBox, Me.Label2, Me.Label1, Me.CounterListBox, Me.ObjectListBox})
        Me.Name = "Form1"
        Me.Text = "Performance1 - System.Diagnostiocs Performance Objects"
        Me.ResumeLayout(False)

    End Sub

#End Region
    Private counters As New ArrayList()
    Private counter As New PerformanceCounter()
    Private cat As New PerformanceCounterCategory()

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim categories As PerformanceCounterCategory()
        ObjectListBox.Sorted = True
        ObjectListBox.Items.Clear()
        categories = PerformanceCounterCategory.GetCategories
        Dim i As Integer
        For i = 0 To categories.Length - 1
            ObjectListBox.Items.Add(categories(i).CategoryName)
        Next
    End Sub

    Private Sub ObjectListBox_SelectedIndexChanged(ByVal sender _
    As System.Object, ByVal e As System.EventArgs) _
    Handles ObjectListBox.SelectedIndexChanged
        InstanceListBox.Items.Clear()
        CounterListBox.Items.Clear()
        Dim i As Integer
        Dim instances() As String
        cat = New PerformanceCounterCategory( _
        ObjectListBox.SelectedItem.ToString())
        Try
            instances = cat.GetInstanceNames()
            If instances.Length = 0 Then 'There are no instances for this object
                Dim counter As New PerformanceCounter()
                For Each counter In cat.GetCounters()
                    CounterListBox.Items.Add(counter.CounterName)
                Next
            Else
                'There ARE instances for this object so fill instanceListBox
                For i = 0 To instances.Length - 1
                    InstanceListBox.Items.Add(instances(i).ToString())
                Next
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub InstanceListBox_SelectedIndexChanged( _
    ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles InstanceListBox.SelectedIndexChanged
        CounterListBox.Items.Clear()
        cat = New PerformanceCounterCategory(ObjectListBox.SelectedItem.ToString())

        CounterListBox.Items.Clear()
        Dim i As Integer
        counters.AddRange(cat.GetCounters(InstanceListBox.SelectedItem.ToString()))
        For Each counter In counters
            CounterListBox.Items.Add(counter.CounterName)
        Next

    End Sub


    Private Sub CounterListBox_SelectedIndexChanged(ByVal sender _
    As System.Object, ByVal e As System.EventArgs) _
    Handles CounterListBox.SelectedIndexChanged
        Me.CounterValueTextBox.Text = CType(counters(CounterListBox.SelectedIndex), _
        PerformanceCounter).RawValue.ToString()
    End Sub
End Class








15.4.Chart
15.4.1.Custom ChartCustom Chart
15.4.2.Char CPU and MemoryChar CPU and Memory
15.4.3.Performance Monitor
15.4.4.GDI+ Line ChartGDI+ Line Chart
15.4.5.Thread GraphThread Graph