VB.Net Tutorial/GUI Applications/Chart — различия между версиями

Материал из VB Эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 16:40, 26 мая 2010

Char CPU and Memory

"Sams Teach Yourself Visual Basic .NET in 21 Days
"By Lowell Mauer
"Published 2001
"Sams Publishing
"ISBN 0672322714

Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Drawing2D
public class CharCPUMemory
   public Shared Sub Main
        Application.Run(New frmMain)
   End Sub
End class

Public Class frmMain
    Inherits System.Windows.Forms.Form
    Dim m_sngX As Single
    Dim m_sngY As Single
    Dim m_sngCPUY As Single
    Dim m_sngProcsY As Single
    Dim m_sngMemY As Single
    Dim penCPU As New System.Drawing.Pen(Color.Red)
    Dim penProcs As New System.Drawing.Pen(Color.Green)
    Dim penMem As New System.Drawing.Pen(Color.Blue)
    Dim penFore As New System.Drawing.Pen(SystemColors.WindowText)
    Dim penBack As New System.Drawing.Pen(SystemColors.Control)
    Dim MachineName As String = System.Windows.Forms.SystemInformation.ruputerName
    Const INCREMENT As Single = 1
#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
    Friend WithEvents pnlSweep As System.Windows.Forms.Panel
    Friend WithEvents cmdSweep As System.Windows.Forms.Button
    Friend WithEvents trkSpeed As System.Windows.Forms.TrackBar
    Friend WithEvents lblCPU As System.Windows.Forms.Label
    Friend WithEvents lblProc As System.Windows.Forms.Label
    Friend WithEvents lblMem As System.Windows.Forms.Label
    Friend WithEvents tmrClock As System.Windows.Forms.Timer
#Region "Declaration of Performance counters"
    Friend WithEvents prfCPU As System.Diagnostics.PerformanceCounter
    Friend WithEvents prfProcs As System.Diagnostics.PerformanceCounter
    Friend WithEvents prfMem As System.Diagnostics.PerformanceCounter
#End Region
    Friend WithEvents logApp As System.Diagnostics.EventLog
    Private components As System.ruponentModel.IContainer
    "Required by the Windows Form Designer
    "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.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.ruponents = New System.ruponentModel.Container()
        Me.prfCPU = New System.Diagnostics.PerformanceCounter()
        Me.pnlSweep = New System.Windows.Forms.Panel()
        Me.tmrClock = New System.Windows.Forms.Timer(Me.ruponents)
        Me.trkSpeed = New System.Windows.Forms.TrackBar()
        Me.lblProc = New System.Windows.Forms.Label()
        Me.prfProcs = New System.Diagnostics.PerformanceCounter()
        Me.lblMem = New System.Windows.Forms.Label()
        Me.prfMem = New System.Diagnostics.PerformanceCounter()
        Me.lblCPU = New System.Windows.Forms.Label()
        Me.cmdSweep = New System.Windows.Forms.Button()
        Me.logApp = New System.Diagnostics.EventLog()
        CType(Me.prfCPU, System.ruponentModel.ISupportInitialize).BeginInit()
        CType(Me.trkSpeed, System.ruponentModel.ISupportInitialize).BeginInit()
        CType(Me.prfProcs, System.ruponentModel.ISupportInitialize).BeginInit()
        CType(Me.prfMem, System.ruponentModel.ISupportInitialize).BeginInit()
        CType(Me.logApp, System.ruponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        "***Initializing Performance Counters"
        "
        "prfCPU
        "
        Me.prfCPU.CategoryName = "Processor"
        Me.prfCPU.CounterName = "% Processor Time"
        Me.prfCPU.InstanceName = "_Total"
        Me.prfCPU.MachineName = MachineName
        "
        "prfProcs
        "
        Me.prfProcs.CategoryName = "System"
        Me.prfProcs.CounterName = "Processes"
        Me.prfProcs.MachineName = MachineName
        "
        "prfMem
        "
        Me.prfMem.CategoryName = "Memory"
        Me.prfMem.CounterName = "Committed Bytes"
        Me.prfMem.MachineName = MachineName
        "
        "pnlSweep
        "
        Me.pnlSweep.Dock = System.Windows.Forms.DockStyle.Top
        Me.pnlSweep.Name = "pnlSweep"
        Me.pnlSweep.Size = New System.Drawing.Size(472, 240)
        Me.pnlSweep.TabIndex = 0
        "
        "tmrClock
        "
        Me.tmrClock.Interval = 1000
        "
        "trkSpeed
        "
        Me.trkSpeed.Location = New System.Drawing.Point(8, 280)
        Me.trkSpeed.Maximum = 20
        Me.trkSpeed.Minimum = 1
        Me.trkSpeed.Name = "trkSpeed"
        Me.trkSpeed.TabIndex = 2
        Me.trkSpeed.Value = 10
        "
        "lblProc
        "
        Me.lblProc.AutoSize = True
        Me.lblProc.ForeColor = System.Drawing.Color.Green
        Me.lblProc.Location = New System.Drawing.Point(240, 272)
        Me.lblProc.Name = "lblProc"
        Me.lblProc.Size = New System.Drawing.Size(60, 13)
        Me.lblProc.TabIndex = 3
        Me.lblProc.Text = "Processes:"
        "
        "lblMem
        "
        Me.lblMem.AutoSize = True
        Me.lblMem.ForeColor = System.Drawing.Color.Blue
        Me.lblMem.Location = New System.Drawing.Point(240, 296)
        Me.lblMem.Name = "lblMem"
        Me.lblMem.Size = New System.Drawing.Size(48, 13)
        Me.lblMem.TabIndex = 3
        Me.lblMem.Text = "Memory:"
        "
        "lblCPU
        "
        Me.lblCPU.AutoSize = True
        Me.lblCPU.ForeColor = System.Drawing.Color.Red
        Me.lblCPU.Location = New System.Drawing.Point(240, 248)
        Me.lblCPU.Name = "lblCPU"
        Me.lblCPU.Size = New System.Drawing.Size(31, 13)
        Me.lblCPU.TabIndex = 3
        Me.lblCPU.Text = "CPU:"
        "
        "cmdSweep
        "
        Me.cmdSweep.Location = New System.Drawing.Point(8, 248)
        Me.cmdSweep.Name = "cmdSweep"
        Me.cmdSweep.Size = New System.Drawing.Size(64, 24)
        Me.cmdSweep.TabIndex = 1
        Me.cmdSweep.Text = "&Start"
        "
        "logApp
        "
        Me.logApp.Log = "Application"
        Me.logApp.MachineName = MachineName
        Me.logApp.Source = "PerfLite"
        Me.logApp.SynchronizingObject = Me
        "
        "frmMain
        "
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(472, 333)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblCPU, Me.trkSpeed, Me.cmdSweep, Me.pnlSweep, Me.lblProc, Me.lblMem})
        Me.Name = "frmMain"
        Me.Text = "PerfLite"
        CType(Me.prfCPU, System.ruponentModel.ISupportInitialize).EndInit()
        CType(Me.trkSpeed, System.ruponentModel.ISupportInitialize).EndInit()
        CType(Me.prfProcs, System.ruponentModel.ISupportInitialize).EndInit()
        CType(Me.prfMem, System.ruponentModel.ISupportInitialize).EndInit()
        CType(Me.logApp, System.ruponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)
    End Sub
#End Region
    Private Sub tmrClock_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrClock.Tick
        Dim sngCPU As Single = prfCPU.NextValue() / 100
        Dim sngProcs As Single = prfProcs.NextValue()
        Dim sngMem As Single = prfMem.NextValue / 1024
        "draw the sweep panel
        DrawSweep(sngCPU, sngProcs, sngMem)
        "update the labels
        lblCPU.Text = "CPU: " & sngCPU.ToString("p")
        lblProc.Text = "Processes: " & sngProcs
        lblMem.Text = "Memory: " & sngMem.ToString("f0") & "KB"
    End Sub
    Private Sub DrawSweep(ByVal CPU As Single, _
        ByVal Processes As Single, _
        ByVal Memory As Single)
        Dim oGrafix As Graphics = pnlSweep.CreateGraphics()
        Dim sngHeight As Single = pnlSweep.Height
        "for the points
        Dim sngCPUY As Single
        Dim sngProcsY As Single
        Dim sngMemY As Single
        "erase previous sweep line
        oGrafix.DrawLine(penBack, m_sngX, 0, m_sngX, sngHeight)
        "draw data points
        sngCPUY = sngHeight - (CPU * sngHeight) - 1
        oGrafix.DrawLine(penCPU, _
            m_sngX - INCREMENT, m_sngCPUY, m_sngX, sngCPUY)
        m_sngCPUY = sngCPUY
        sngProcsY = sngHeight - Processes
        oGrafix.DrawLine(penProcs, _
            m_sngX - INCREMENT, m_sngProcsY, m_sngX, sngProcsY)
        m_sngProcsY = sngProcsY
        "the 10000 is for my machine, to get the memory to a nice value
        "   you may need to change it if the memory line
        "   isn"t drawing correctly
        sngMemY = sngHeight - (Memory / 10000)
        oGrafix.DrawLine(penMem, _
            m_sngX - INCREMENT, m_sngMemY, m_sngX, sngMemY)
        m_sngMemY = sngMemY
        "increment x
        m_sngX += INCREMENT
        If m_sngX > pnlSweep.Width Then
            "reset back to start
            m_sngX = 0
            "and clear the drawing surface
            oGrafix.Clear(SystemColors.Control)
        End If
        "draw new line
        oGrafix.DrawLine(penFore, m_sngX, 0, m_sngX, sngHeight)
    End Sub
    Private Sub cmdSweep_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSweep.Click
        "Toggle the button text and timer (on or off)
        If cmdSweep.Text = "&Start" Then
            cmdSweep.Text = "&Stop"
            tmrClock.Enabled = True
        Else
            cmdSweep.Text = "&Start"
            tmrClock.Enabled = False
        End If
    End Sub
    Private Sub trkSpeed_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles trkSpeed.Scroll
        Dim iValue As Integer
        iValue = CInt(trkSpeed.Value)
        "set the timer interval to the selected time
        tmrClock.Interval = iValue * 100 "ms
    End Sub
End Class

Custom Chart

" Quote from
"Visual Basic 2005 Cookbook Solutions for VB 2005 Programmers
"by Tim Patrick (Author), John Craig (Author)
"# Publisher: O"Reilly Media, Inc. (September 21, 2006)
"# Language: English
"# ISBN-10: 0596101775
"# ISBN-13: 978-0596101770

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class CustomChart
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class

Public Class Form1
    Private Sub Form1_Paint(ByVal sender As Object, _
          ByVal e As System.Windows.Forms.PaintEventArgs) _
          Handles Me.Paint
        " ----- Draw a nice chart.
        Dim canvas As Graphics = e.Graphics
        " ----- Create an array of data points to plot.
        Dim chartData() As Single = _
           {20, 33, 44, 25, 17, 24, 63, 75, 54, 33}
        " ----- Create some pens.
        Dim penRed As New Pen(Color.Red, -1)
        Dim penBlack As New Pen(Color.Black, -1)
        Dim penShadow As New Pen(Color.Gray, -1)
        " ----- Prepare to add labels.
        Dim labelFont As New Font("Arial", 3, FontStyle.Regular)
        Dim labelBrush As New SolidBrush(Color.Blue)
        " ----- Used to plot the various elements.
        Dim x1, y1 As Single "Lower left corner
        Dim x2, y2 As Single "Upper right corner
        Dim scaleX, scaleY As Single
        Dim xScan, yScan As Single
        Dim oneBar As RectangleF
        " ----- Set the scaling.
        x1 = -10
        y1 = -10
        x2 = 110
        y2 = 110
        scaleX = Me.ClientSize.Width / (x2 - x1)
        scaleY = Me.ClientSize.Height / (y2 - y1)
        canvas.ScaleTransform(scaleX, -scaleY) "(inverted)
        canvas.TranslateTransform(-x1, -y2)    "(inverted)
        " ----- Color the background.
        canvas.Clear(Color.Cornsilk)
        " ----- Draw chart outline rectangle.
        canvas.DrawRectangle(penBlack, New Rectangle(0, 0, 100, 100))
        " ----- Draw the chart grid.
        For xScan = 10 To 90 Step 10
            canvas.DrawLine(penBlack, xScan, 0, xScan, 100)
        Next xScan
        For yScan = 10 To 90 Step 10
            canvas.DrawLine(penBlack, 0, yScan, 100, yScan)
        Next yScan
        " ----- Draw some shadowed bars.
        For xScan = 0 To 90 Step 10
            " ----- Draw the shadow first.
            oneBar.X = xScan + 0.6
            oneBar.Y = 0
            oneBar.Width = 6
            oneBar.Height = chartData(xScan \ 10) - 2
            canvas.FillRectangle(New SolidBrush(Color.FromArgb(127, _
               Color.Gray)), oneBar)
            " ----- Now draw the bar in front.
            oneBar.X = xScan + 2
            oneBar.Y = 0
            oneBar.Height = chartData(xScan \ 10)
            canvas.FillRectangle(New SolidBrush(Color.Red), oneBar)
        Next xScan
        " ----- Need to un-invert the scaling so text labels are
        "       right-side-up.
        canvas.ResetTransform()
        canvas.ScaleTransform(scaleX, scaleY)
        canvas.TranslateTransform(-x1, -y1)
        " ----- Label the Y axis.
        For yScan = 0 To 100 Step 10
            canvas.DrawString(yScan.ToString, labelFont, labelBrush, _
               -2 * yScan.ToString.Length - 3, 97 - yScan)
        Next yScan
        " ----- Label the X axis
        For xScan = 0 To 100 Step 10
            canvas.DrawString(xScan.ToString, labelFont, labelBrush, _
               xScan + 1.7 - 2 * xScan.ToString.Length, 103)
        Next xScan
        " ----- Cleanup.
        labelFont.Dispose()
        labelBrush.Dispose()
        penRed.Dispose()
        penBlack.Dispose()
        penShadow.Dispose()
        canvas = Nothing
    End Sub
    Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        " ----- Refresh on resize.
        Me.Refresh()
    End Sub
End Class

<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form
    "Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub
    "Required by the Windows Form Designer
    Private components As System.ruponentModel.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.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.SuspendLayout()
        "
        "Form1
        "
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(549, 394)
        Me.DoubleBuffered = True
        Me.Name = "Form1"
        Me.Text = "Drawing a Simple Chart"
        Me.ResumeLayout(False)
    End Sub
End Class

GDI+ Line Chart

Imports System
Imports System.Drawing
Imports System.Collections
Imports System.ruponentModel
Imports System.Windows.Forms
Imports System.Data
Imports System.Drawing.Drawing2D
public class DrawCoordinate
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class
Public Class Form1
    Inherits System.Windows.Forms.Form
    Private startPoint As New Point(50, 217)
    Private endPoint As New Point(50, 217)
    Public Sub New()
        MyBase.New()
        InitializeComponent()
    End Sub
    Private components As System.ruponentModel.IContainer
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents CheckBox1 As System.Windows.Forms.CheckBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.CheckBox1 = New System.Windows.Forms.CheckBox
        Me.SuspendLayout()
        "
        "Button1
        "
        Me.Button1.BackColor = System.Drawing.SystemColors.Desktop
        Me.Button1.Location = New System.Drawing.Point(352, 16)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(96, 24)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Clear All"
        "
        "CheckBox1
        "
        Me.CheckBox1.Location = New System.Drawing.Point(352, 56)
        Me.CheckBox1.Name = "CheckBox1"
        Me.CheckBox1.TabIndex = 1
        Me.CheckBox1.Text = "Rectangle"
        "
        "Form1
        "
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.BackColor = System.Drawing.SystemColors.ActiveCaptionText
        Me.ClientSize = New System.Drawing.Size(480, 317)
        Me.Controls.Add(Me.CheckBox1)
        Me.Controls.Add(Me.Button1)
        Me.Text = ""
        Me.ResumeLayout(False)
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        startPoint.X = 50
        startPoint.Y = 217
        endPoint.X = 50
        endPoint.Y = 217
        Me.Invalidate(Me.ClientRectangle)
    End Sub
    Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)
        If e.Button = MouseButtons.Left Then
            Dim g1 As Graphics = Me.CreateGraphics()
            Dim linePen As New Pen(Color.Green, 1)
            Dim ellipsePen As New Pen(Color.Red, 1)
            startPoint = endPoint
            endPoint = New Point(e.X, e.Y)
            g1.DrawLine(linePen, startPoint, endPoint)
            If checkBox1.Checked Then
                g1.DrawRectangle(ellipsePen, e.X - 2, e.Y - 2, 4, 4)
            Else
                g1.DrawEllipse(ellipsePen, e.X - 2, e.Y - 2, 4, 4)
            End If
            linePen.Dispose()
            ellipsePen.Dispose()
            g1.Dispose()
        End If
    End Sub
    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
        Dim g As Graphics = e.Graphics
        Dim vertFont As New Font("Verdana", 10, FontStyle.Bold)
        Dim horzFont As New Font("Verdana", 10, FontStyle.Bold)
        Dim vertBrush As New SolidBrush(Color.Black)
        Dim horzBrush As New SolidBrush(Color.Blue)
        Dim blackPen As New Pen(Color.Black, 2)
        Dim bluePen As New Pen(Color.Blue, 2)
        " Drawing a vertical and a horizontal line
        g.DrawLine(blackPen, 50, 220, 50, 25)
        g.DrawLine(bluePen, 50, 220, 250, 220)
        "X axis drawing
        g.DrawString("0", horzFont, horzBrush, 30, 220)
        g.DrawString("1", horzFont, horzBrush, 50, 220)
        g.DrawString("2", horzFont, horzBrush, 70, 220)
        g.DrawString("3", horzFont, horzBrush, 90, 220)
        g.DrawString("4", horzFont, horzBrush, 110, 220)
        g.DrawString("5", horzFont, horzBrush, 130, 220)
        g.DrawString("6", horzFont, horzBrush, 150, 220)
        g.DrawString("7", horzFont, horzBrush, 170, 220)
        g.DrawString("8", horzFont, horzBrush, 190, 220)
        g.DrawString("9", horzFont, horzBrush, 210, 220)
        g.DrawString("10", horzFont, horzBrush, 230, 220)
        " Drawing vertical strings
        Dim vertStrFormat As New StringFormat
        vertStrFormat.FormatFlags = StringFormatFlags.DirectionVertical
        g.DrawString("-", horzFont, horzBrush, 50, 212, vertStrFormat)
        g.DrawString("-", horzFont, horzBrush, 70, 212, vertStrFormat)
        g.DrawString("-", horzFont, horzBrush, 90, 212, vertStrFormat)
        g.DrawString("-", horzFont, horzBrush, 110, 212, vertStrFormat)
        g.DrawString("-", horzFont, horzBrush, 130, 212, vertStrFormat)
        g.DrawString("-", horzFont, horzBrush, 150, 212, vertStrFormat)
        g.DrawString("-", horzFont, horzBrush, 170, 212, vertStrFormat)
        g.DrawString("-", horzFont, horzBrush, 190, 212, vertStrFormat)
        g.DrawString("-", horzFont, horzBrush, 210, 212, vertStrFormat)
        g.DrawString("-", horzFont, horzBrush, 230, 212, vertStrFormat)
        "Y axis drawing
        g.DrawString("100-", vertFont, vertBrush, 20, 20)
        g.DrawString("90 -", vertFont, vertBrush, 25, 40)
        g.DrawString("80 -", vertFont, vertBrush, 25, 60)
        g.DrawString("70 -", vertFont, vertBrush, 25, 80)
        g.DrawString("60 -", vertFont, vertBrush, 25, 100)
        g.DrawString("50 -", vertFont, vertBrush, 25, 120)
        g.DrawString("40 -", vertFont, vertBrush, 25, 140)
        g.DrawString("30 -", vertFont, vertBrush, 25, 160)
        g.DrawString("20 -", vertFont, vertBrush, 25, 180)
        g.DrawString("10 -", vertFont, vertBrush, 25, 200)
        " Dispose
        vertFont.Dispose()
        horzFont.Dispose()
        vertBrush.Dispose()
        horzBrush.Dispose()
        blackPen.Dispose()
        bluePen.Dispose()
    End Sub
End Class

Performance Monitor

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.ruponentModel.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

Thread Graph

"Visual Basic 2005 Programmer"s Reference
"by Rod Stephens (Author) 
"# Publisher: Wrox (October 21, 2005)
"# Language: English
"# ISBN-10: 0764571982
"# ISBN-13: 978-0764571985

Imports System.Drawing
Imports System.Windows.Forms
Imports System.Math
Imports System.Threading
public class ThreadGraph
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class

Public Class Form1
    Private m_Ymid As Integer
    Private m_Y As Integer
    Private Const GRID_STEP As Integer = 40
    Private m_GraphThread As Thread
    " Get ready.
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        m_Ymid = picGraph.ClientSize.Height \ 2
        m_Y = m_Ymid
        " Make the Bitmap and Graphics objects.
        Dim wid As Integer = picGraph.ClientSize.Width
        Dim hgt As Integer = picGraph.ClientSize.Height
        Dim bm As New Bitmap(wid, hgt)
        Dim gr As Graphics = Graphics.FromImage(bm)
        " Draw guide lines.
        gr.Clear(Color.Blue)
        For i As Integer = m_Ymid To picGraph.ClientSize.Height Step GRID_STEP
            gr.DrawLine(Pens.LightBlue, 0, i, wid - 1, i)
        Next i
        For i As Integer = m_Ymid To 0 Step -GRID_STEP
            gr.DrawLine(Pens.LightBlue, 0, i, wid - 1, i)
        Next i
        picGraph.Image = bm
    End Sub
    " Start drawing the graph.
    Private Sub btnGraph_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGraph.Click
        If m_GraphThread Is Nothing Then
            " The thread isn"t running. Start it.
            AddStatus("Starting thread")
            m_GraphThread = New Thread(AddressOf DrawGraph)
            m_GraphThread.Priority = ThreadPriority.BelowNormal
            m_GraphThread.IsBackground = True
            m_GraphThread.Start()
            AddStatus("Thread started")
            btnGraph.Text = "Stop"
        Else
            " The thread is running. Stop it.
            AddStatus("Stopping thread")
            m_GraphThread.Abort()
            " m_GraphThread.Join()
            m_GraphThread = Nothing
            AddStatus("Thread stopped")
            btnGraph.Text = "Start"
        End If
    End Sub
    " Draw a graph until stopped.
    Private Sub DrawGraph()
        Try
            " Generate pseudo-random values.
            Dim y As Integer = m_Y
            Do
                " Generate the next value.
                NewValue()
                " Plot the new value.
                PlotValue(y, m_Y)
                y = m_Y
            Loop
        Catch ex As Exception
            AddStatus("[Thread] " & ex.Message)
        End Try
    End Sub
    " Generate the next value.
    Private Sub NewValue()
        " Delay a bit before calculating the value.
        Dim stop_time As Date = Now.AddMilliseconds(20)
        Do While Now < stop_time
        Loop
        " Calculate the next value.
        Static rnd As New Random
        m_Y += rnd.Next(-4, 5)
        If m_Y < 0 Then m_Y = 0
        If m_Y >= picGraph.ClientSize.Height - 1 Then m_Y = picGraph.ClientSize.Height - 1
    End Sub
    " Plot a new value.
    Private Delegate Sub PlotValueDelegate(ByVal old_y As Integer, ByVal new_y As Integer)
    Private Sub PlotValue(ByVal old_y As Integer, ByVal new_y As Integer)
        " See if we"re on the worker thread and thus
        " need to invoke the main UI thread.
        If Me.InvokeRequired Then
            " Make arguments for the delegate.
            Dim args As Object() = {old_y, new_y}
            " Make the delegate.
            Dim plot_value_delegate As PlotValueDelegate
            plot_value_delegate = AddressOf PlotValue
            " Invoke the delegate on the main UI thread.
            Me.Invoke(plot_value_delegate, args)
            " We"re done.
            Exit Sub
        End If
        " Make the Bitmap and Graphics objects.
        Dim wid As Integer = picGraph.ClientSize.Width
        Dim hgt As Integer = picGraph.ClientSize.Height
        Dim bm As New Bitmap(wid, hgt)
        Dim gr As Graphics = Graphics.FromImage(bm)
        " Move the old data one pixel to the left.
        gr.DrawImage(picGraph.Image, -1, 0)
        " Erase the right edge and draw guide lines.
        gr.DrawLine(Pens.Blue, wid - 1, 0, wid - 1, hgt - 1)
        For i As Integer = m_Ymid To picGraph.ClientSize.Height Step GRID_STEP
            gr.DrawLine(Pens.LightBlue, wid - 2, i, wid - 1, i)
        Next i
        For i As Integer = m_Ymid To 0 Step -GRID_STEP
            gr.DrawLine(Pens.LightBlue, wid - 2, i, wid - 1, i)
        Next i
        " Plot a new pixel.
        gr.DrawLine(Pens.White, wid - 2, old_y, wid - 1, new_y)
        " Display the result.
        picGraph.Image = bm
        picGraph.Refresh()
        gr.Dispose()
    End Sub
    " Add a status string to txtStatus.
    Private Delegate Sub AddStatusDelegate(ByVal txt As String)
    Private Sub AddStatus(ByVal txt As String)
        " See if we"re on the worker thread and thus
        " need to invoke the main UI thread.
        If Me.InvokeRequired Then
            " Make arguments for the delegate.
            Dim args As Object() = {txt}
            " Make the delegate.
            Dim add_status_delegate As AddStatusDelegate
            add_status_delegate = AddressOf AddStatus
            " Invoke the delegate on the main UI thread.
            Me.Invoke(add_status_delegate, args)
            " We"re done.
            Exit Sub
        End If
        txtStatus.Text &= vbCrLf & txt
        txtStatus.Select(txtStatus.Text.Length, 0)
        txtStatus.ScrollToCaret()
    End Sub
    " Display the current time.
    Private Sub tmrUpdateClock_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrUpdateClock.Tick
        lblTime.Text = Now.ToString("T")
    End Sub
End Class
<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _
Partial Public Class Form1
    Inherits System.Windows.Forms.Form
    "Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub
    "Required by the Windows Form Designer
    Private components As System.ruponentModel.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.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.ruponents = New System.ruponentModel.Container
        Me.lblTime = New System.Windows.Forms.Label
        Me.txtStatus = New System.Windows.Forms.TextBox
        Me.picGraph = New System.Windows.Forms.PictureBox
        Me.btnGraph = New System.Windows.Forms.Button
        Me.tmrUpdateClock = New System.Windows.Forms.Timer(Me.ruponents)
        CType(Me.picGraph, System.ruponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        "
        "lblTime
        "
        Me.lblTime.Location = New System.Drawing.Point(208, 4)
        Me.lblTime.Name = "lblTime"
        Me.lblTime.Size = New System.Drawing.Size(80, 16)
        Me.lblTime.TabIndex = 7
        Me.lblTime.Text = "Label1"
        Me.lblTime.TextAlign = System.Drawing.ContentAlignment.MiddleRight
        "
        "txtStatus
        "
        Me.txtStatus.Dock = System.Windows.Forms.DockStyle.Bottom
        Me.txtStatus.Location = New System.Drawing.Point(0, 38)
        Me.txtStatus.Multiline = True
        Me.txtStatus.Name = "txtStatus"
        Me.txtStatus.ReadOnly = True
        Me.txtStatus.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.txtStatus.Size = New System.Drawing.Size(292, 85)
        Me.txtStatus.TabIndex = 6
        Me.txtStatus.Text = "Ready"
        "
        "picGraph
        "
        Me.picGraph.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
        Me.picGraph.Dock = System.Windows.Forms.DockStyle.Bottom
        Me.picGraph.Location = New System.Drawing.Point(0, 123)
        Me.picGraph.Name = "picGraph"
        Me.picGraph.Size = New System.Drawing.Size(292, 150)
        Me.picGraph.TabIndex = 5
        Me.picGraph.TabStop = False
        "
        "btnGraph
        "
        Me.btnGraph.Location = New System.Drawing.Point(8, 4)
        Me.btnGraph.Name = "btnGraph"
        Me.btnGraph.Size = New System.Drawing.Size(48, 24)
        Me.btnGraph.TabIndex = 4
        Me.btnGraph.Text = "Graph"
        "
        "tmrUpdateClock
        "
        Me.tmrUpdateClock.Enabled = True
        Me.tmrUpdateClock.Interval = 250
        "
        "Form1
        "
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Controls.Add(Me.lblTime)
        Me.Controls.Add(Me.txtStatus)
        Me.Controls.Add(Me.picGraph)
        Me.Controls.Add(Me.btnGraph)
        Me.Name = "Form1"
        Me.Text = "ThreadGraph"
        CType(Me.picGraph, System.ruponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)
        Me.PerformLayout()
    End Sub
    Friend WithEvents lblTime As System.Windows.Forms.Label
    Friend WithEvents txtStatus As System.Windows.Forms.TextBox
    Friend WithEvents picGraph As System.Windows.Forms.PictureBox
    Friend WithEvents btnGraph As System.Windows.Forms.Button
    Friend WithEvents tmrUpdateClock As System.Windows.Forms.Timer
End Class