A simple but very useable Perfomance Monitor with all Managed Code.

 

This is a simple performance monitor which is made to run all the time on my system. It has a rolling display supporting single core, hyperthreaded and dual-core processors running on XP and Vista (administrator privileges required). It offers contnuous real-time displays of:

 

·         Percent processor usage for each core.

·         Percentage of physical memory in use

·         Percentage of available virtual memory in use.

  

Right clicking on the monitor screen evokes a context menu for setting sampling intervals of 250, 500, 1000 and 5000 milliseconds. It also allows the user to choose whether the memory data is collected and displayed.

 

Support for mouse capture is included at the edges of the form. Form location is saved in a settings file as are the other menu settings. The code itself appears to be highly efficient, showing no measureable processor load.

 

If you are interested, make a windows form project and borderless form. The size of the form should be 204 x 29 pixels. To that form, add a picturebox and name it pb1. I set Form1's backcolor to 0,0,64. That’s the total preparation needed for the form.

 

Source code follows:

 

 

Imports System.data

Imports System.diagnostics

Imports System.Runtime.InteropServices

 

Public Class Form1

    Protected Friend WithEvents Tmr As New Timer

    Dim XOffset As Integer

    Dim YOffset As Integer

    Dim FormLocation As Point

    Private MouseDwn As Boolean

    Private pbLoc As Point

    Private P0 As New System.Diagnostics.PerformanceCounter

    Private P1 As New System.Diagnostics.PerformanceCounter

    Private VMInUse As New System.Diagnostics.PerformanceCounter

    Private ShowVirtMem As Boolean = My.Settings.ShowVirtualMemory

    Private ShowPhysMem As Boolean = My.Settings.ShowPhysicalMemory

    Private TotPhysicalMem As ULong = My.Computer.Info.TotalPhysicalMemory

    Private AvailPhysicalMem As ULong

    Const RectangleWidth As Integer = 200 : Const RectangleHeight As Integer = 25

    Private StepSize As Integer = 2

    Private Uni As Boolean = System.Environment.ProcessorCount() = 1

    Private bm As Bitmap = My.Resources.Image1

    Private nbm As Bitmap

    Private StorageBitmap As Graphics = Graphics.FromImage(bm)

    Protected Friend WithEvents CtxMenu As New ContextMenuStrip

    Protected Friend WithEvents Int250 As New ToolStripMenuItem("250 ms")

    Protected Friend WithEvents Int500 As New ToolStripMenuItem("500 ms")

    Protected Friend WithEvents Int1000 As New ToolStripMenuItem(" 1 Sec")

    Protected Friend WithEvents Int5000 As New ToolStripMenuItem(" 5 Secs")

    Protected Friend WithEvents AvailPhysMemInUse As New ToolStripMenuItem("Physical Memory in Use")

    Protected Friend WithEvents AvailVirtMemInUse As New ToolStripMenuItem("Virtual Memory in Use")

    Protected Friend WithEvents MnuExit As New ToolStripMenuItem("Exit")

    Protected Friend WithEvents NotifyIcon As New NotifyIcon

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        pb1.Size = New Size(RectangleWidth, RectangleHeight) : pb1.Location = New Point(2, 2)

        pb1.Margin = New Padding(0)

        pb1.Image = My.Resources.Image1

        Me.TopMost = True

        If My.Settings.FormLocation.X = 0 Then

            Me.Location = New Point _

                                       ((SystemInformation.WorkingArea.Width - Me.Width) - 80, 4)

        Else

            Me.Location = My.Settings.FormLocation

        End If

        Me.ShowInTaskbar = False : InitMenu() : NotifyIcon.BalloonTipTitle = "Pmon" : Dim rv As Int64

        pb1.Location = New Point(2, 3) : Application.DoEvents()

        NotifyIcon.Visible = True : NotifyIcon.Icon = Me.Icon : NotifyIcon.ContextMenuStrip = CtxMenu

        Tmr.Interval = 250 : Tmr.Start()

 

        P0.CategoryName = "Processor" : P0.CounterName = "% Processor Time" : P0.InstanceName = "0"

        If Uni Then

            StepSize = 1

        Else

            P1.CategoryName = "Processor" : P1.CounterName = "% Processor Time" : P1.InstanceName = "1"

            rv = P1.NextValue()

        End If

        VMInUse.CategoryName = "Paging File" : VMInUse.CounterName = "% Usage"

        VMInUse.InstanceName = "_Total" : VMInUse.NextValue()

        rv = P0.NextValue() : Me.Size = New Size(RectangleWidth + 4, RectangleHeight + 6)

    End Sub

  

    Private Sub Tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Tmr.Tick

        Static proc1pen As Pen = New Pen(Color.Red, 1) : Static proc2pen As Pen = New Pen(Color.MintCream, 1)

        Static PhysMemPen As Pen = New Pen(Color.Teal, 1) : Static VirtMemPen As Pen = New Pen(Color.DarkGoldenrod, 1)

        Static clearpen As Pen = New Pen(Color.Black, 2)

        Static P0val As Integer : Static P1val As Integer : Static hDraw As Short : Static YPoint As Short

        hDraw = RectangleHeight - StepSize

        nbm = bm.Clone

        StorageBitmap.DrawImage(nbm, New Rectangle(0, 0, RectangleWidth - StepSize, RectangleHeight), _

                                     New Rectangle(StepSize, 0, RectangleWidth - StepSize, RectangleHeight), _

                                                                 GraphicsUnit.Pixel)

        StorageBitmap.DrawLine(clearpen, RectangleWidth - 1, RectangleHeight, RectangleWidth - 1, 0) 'clear last two lines

        P0val = P0.NextValue() >> 2

        If Uni Then

            StorageBitmap.DrawLine(proc1pen, RectangleWidth - 1, RectangleHeight, RectangleWidth - 1, RectangleHeight - P0val)

        Else

            P1val = P1.NextValue() >> 2

            StorageBitmap.DrawLine(proc1pen, RectangleWidth - 2, RectangleHeight, RectangleWidth - 2, RectangleHeight - P0val)

            StorageBitmap.DrawLine(proc2pen, RectangleWidth - 1, RectangleHeight, RectangleWidth - 1, RectangleHeight - P1val)

        End If

 

        If ShowPhysMem Then

            YPoint = -CInt((My.Computer.Info.AvailablePhysicalMemory / TotPhysicalMem) * -RectangleHeight)

            StorageBitmap.DrawLine(PhysMemPen, RectangleWidth - StepSize, YPoint, RectangleWidth, YPoint)

        End If

        Const Factor As Single = RectangleHeight / 100

        If ShowVirtMem Then

            YPoint = RectangleHeight - Math.Round(VMInUse.NextValue * Factor)

            StorageBitmap.DrawLine(VirtMemPen, RectangleWidth - StepSize, YPoint, RectangleWidth, YPoint)

        End If

        pb1.Image = bm

    End Sub

    Private Sub CtxMenu_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles CtxMenu.MouseLeave

        CtxMenu.Close()

    End Sub

    Private Sub pb1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pb1.MouseClick

        If Not My.Computer.Mouse.ButtonsSwapped Then

            If e.Button = Windows.Forms.MouseButtons.Left Then Exit Sub

        Else

            If e.Button = Windows.Forms.MouseButtons.Right Then Exit Sub

        End If

        CtxMenu.Margin = New Padding(3, 10, 3, 3)

        CtxMenu.Show()

    End Sub

    Private Sub MenuSelectionDispatcher(ByVal sender As Object, ByVal e As System.EventArgs) _

       Handles Int250.Click, Int500.Click, Int1000.Click, Int5000.Click, AvailPhysMemInUse.Click, _

       AvailVirtMemInUse.Click, MnuExit.Click

        Int250.CheckState = CheckState.Unchecked : Int500.CheckState = CheckState.Unchecked

        Int1000.CheckState = CheckState.Unchecked : Int5000.CheckState = CheckState.Unchecked

        Int250.Enabled = True : Int500.Enabled = True : Int1000.Enabled = True : Int5000.Enabled = True

        Select Case sender.name

            Case "Int250"

                Int250.CheckState = CheckState.Checked

                Int250.Enabled = False

                Tmr.Interval = 250

            Case "Int500"

                Int500.CheckState = CheckState.Checked

                Int500.Enabled = False

                Tmr.Interval = 500

            Case "Int1000"

                Int1000.CheckState = CheckState.Checked

                Int1000.Enabled = False

                Tmr.Interval = 1000

            Case "Int5000"

                Int5000.CheckState = CheckState.Checked

                Int5000.Enabled = False

                Tmr.Interval = 5000

            Case "ShowPhysMem"

                If AvailPhysMemInUse.CheckState = CheckState.Checked Then

                    AvailPhysMemInUse.CheckState = CheckState.Unchecked

                    ShowPhysMem = False

                Else

                    AvailPhysMemInUse.CheckState = CheckState.Checked

                    ShowPhysMem = True

                End If

                My.Settings.ShowPhysicalMemory = ShowPhysMem

                My.Settings.Save()

            Case "ShowVirtMem"

                If AvailVirtMemInUse.CheckState = CheckState.Checked Then

                    AvailVirtMemInUse.CheckState = CheckState.Unchecked

                    ShowVirtMem = False

                Else

                    AvailVirtMemInUse.CheckState = CheckState.Checked

                    ShowVirtMem = True

                End If

                My.Settings.ShowVirtualMemory = ShowVirtMem

                My.Settings.Save()

            Case "MnuExit"

                Tmr.Enabled = False

                Pb1_DoubleClick(sender, e)

        End Select

    End Sub

    Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown

        MouseDwn = True

        XOffset = Windows.Forms.Cursor.Position.X - Me.Left

        YOffset = Windows.Forms.Cursor.Position.Y - Me.Top

    End Sub

    Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp

        MouseDwn = False

        My.Settings.FormLocation = Me.Location

        My.Settings.Save()

    End Sub

    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove

        If Not MouseDwn Then Exit Sub

        FormLocation.X = Windows.Forms.Cursor.Position.X - XOffset

        FormLocation.Y = Windows.Forms.Cursor.Position.Y - YOffset

        Me.Location = FormLocation

    End Sub

 

    Private Sub InitMenu()

        pb1.ContextMenuStrip = CtxMenu

        Int250.Name = "Int250" : Int500.Name = "Int500" : Int1000.Name = "Int1000" : Int5000.Name = "Int5000"

        AvailPhysMemInUse.Name = "ShowPhysMem" : AvailPhysMemInUse.Image = ImageList1.Images("PhysMem")

        AvailVirtMemInUse.Name = "ShowVirtMem" : AvailVirtMemInUse.Image = ImageList1.Images("VirtMem")

        MnuExit.Name = "MnuExit"

        CtxMenu.Items.Add(Int250) : CtxMenu.Items.Add(Int500) : CtxMenu.Items.Add(Int1000) : CtxMenu.Items.Add(Int5000)

        CtxMenu.Items.Add(New ToolStripSeparator) : CtxMenu.Items.Add(AvailPhysMemInUse) : CtxMenu.Items.Add(AvailVirtMemInUse) : CtxMenu.Items.Add(New ToolStripSeparator) : CtxMenu.Items.Add(MnuExit)

        Int250.CheckState = CheckState.Checked : Int250.Enabled = False

    End Sub

   

    Private Sub Pb1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles pb1.DoubleClick

        Tmr.Stop() : Tmr.Dispose()

        NotifyIcon.Visible = False : NotifyIcon.Dispose()

        Application.DoEvents() ' For notify icon

        StorageBitmap.Dispose()

        P1.Dispose() : P0.Dispose()

        nbm.Dispose()

        Application.Exit()

    End Sub

   

End Class

There is also a settings file:

Which looks like this:

< xml version="1.0" encoding="utf-8" >
<configuration>
   
<configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="Pmon.My.MySettings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <userSettings>
        <Pmon.My.MySettings>
            <setting name="ShowPhysicalMemory" serializeAs="String">
                <value>True</value>
            </setting>
            <setting name="ShowVirtualMemory" serializeAs="String">
                <value>True</value>
            </setting>
            <setting name="FormLocation" serializeAs="String">
                <value>996, 11</value>
            </setting>
        </Pmon.My.MySettings>
    </userSettings>
</configuration>

You may just prefer to add them to your project. They all have user scope:

ShowPhysicalMemory Boolean True
ShowVirtualMemory Boolean True
FormLocation Point  
996,11

There is also a project resource which is a 200 x 25 pixel .png image which is solid black. (my.resources.image1)

 




Answer this question

A simple but very useable Perfomance Monitor with all Managed Code.

  • Virat

    Goodness. how screen sizes are changing around these days. Yes It would require just a little bit. Let me see what i can do....

     

    Here you are erode. I apologize for not building that in. Since there were tiny tweaks in several places I’m posting the full source.

    I have one doubt/concern and since I only have one sized screen, I can’t play with it. I’m concerned about the size of the png image when the actual image size becomes larger than the rectangle described by the png image.

    Please let me know if after redoing this, you needed to redo the png resource.

    There is also an experiment you might want to run which is to make that png image considerably larger like by a factor of two from its current size and go ahead and execute the revised code. I believe every thing should size itself if I’m not mistaken.

    Imports System.data< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

    Imports System.diagnostics

    Imports System.Runtime.InteropServices

     

    Public Class Form1

        Protected Friend WithEvents Tmr As New Timer

        Dim XOffset As Integer

        Dim YOffset As Integer

        Dim FormLocation As Point

        Private MouseDwn As Boolean

        Private pbLoc As Point

        Private P0 As New System.Diagnostics.PerformanceCounter

        Private P1 As New System.Diagnostics.PerformanceCounter

        Private VMInUse As New System.Diagnostics.PerformanceCounter

        Private ShowVirtMem As Boolean = My.Settings.ShowVirtualMemory

        Private ShowPhysMem As Boolean = My.Settings.ShowPhysicalMemory

        Private TotPhysicalMem As ULong = My.Computer.Info.TotalPhysicalMemory

        Private AvailPhysicalMem As ULong

        Private RectangleWidth As Integer = 200 * (Screen.PrimaryScreen.Bounds.Width / 1280)

        Private RectangleHeight As Integer = 25 * (Screen.PrimaryScreen.Bounds.Height / 1024)

        Private StepSize As Integer = 2

        Private Uni As Boolean = System.Environment.ProcessorCount() = 1

        Private bm As Bitmap = My.Resources.Image1

        Private nbm As Bitmap

        Private StorageBitmap As Graphics = Graphics.FromImage(bm)

        Protected Friend WithEvents CtxMenu As New ContextMenuStrip

        Protected Friend WithEvents Int250 As New ToolStripMenuItem("250 ms")

        Protected Friend WithEvents Int500 As New ToolStripMenuItem("500 ms")

        Protected Friend WithEvents Int1000 As New ToolStripMenuItem(" 1 Sec")

        Protected Friend WithEvents Int5000 As New ToolStripMenuItem(" 5 Secs")

        Protected Friend WithEvents AvailPhysMemInUse As New ToolStripMenuItem("Physical Memory in Use")

        Protected Friend WithEvents AvailVirtMemInUse As New ToolStripMenuItem("Virtual Memory in Use")

        Protected Friend WithEvents MnuExit As New ToolStripMenuItem("Exit")

        Protected Friend WithEvents NotifyIcon As New NotifyIcon

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

            pb1.Size = New Size(RectangleWidth, RectangleHeight) : pb1.Location = New Point(2, 2)

            pb1.Margin = New Padding(0)

            pb1.Image = My.Resources.Image1

            Me.TopMost = True

            If My.Settings.FormLocation.X = 0 Then

                Me.Location = New Point _

                                           ((SystemInformation.WorkingArea.Width - Me.Width) - 80, 4)

            Else

                Me.Location = My.Settings.FormLocation

            End If

            Me.ShowInTaskbar = False : InitMenu() : NotifyIcon.BalloonTipTitle = "Pmon" : Dim rv As Int64

            pb1.Location = New Point(2, 3) : Application.DoEvents()

            NotifyIcon.Visible = True : NotifyIcon.Icon = Me.Icon : NotifyIcon.ContextMenuStrip = CtxMenu

            Tmr.Interval = 250 : Tmr.Start()

     

            P0.CategoryName = "Processor" : P0.CounterName = "% Processor Time" : P0.InstanceName = "0"

            If Uni Then

                StepSize = 1

            Else

                P1.CategoryName = "Processor" : P1.CounterName = "% Processor Time" : P1.InstanceName = "1"

                rv = P1.NextValue()

            End If

            VMInUse.CategoryName = "Paging File" : VMInUse.CounterName = "% Usage"

            VMInUse.InstanceName = "_Total" : VMInUse.NextValue()

            rv = P0.NextValue() : Me.Size = New Size(RectangleWidth + 4, RectangleHeight + 6)

            Me.Size = New Size(pb1.Width + 4, pb1.Height + 5)

            pb1.Location = New Point(2, 2)

        End Sub

      

        Private Sub Tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Tmr.Tick

            Static Factor As Single = RectangleHeight / 100

            Static proc1pen As Pen = New Pen(Color.Red, 1) : Static proc2pen As Pen = New Pen(Color.MintCream, 1)

            Static PhysMemPen As Pen = New Pen(Color.Teal, 1) : Static VirtMemPen As Pen = New Pen(Color.DarkGoldenrod, 1)

            Static clearpen As Pen = New Pen(Color.Black, 2)

            Static P0val As Integer : Static P1val As Integer : Static hDraw As Short : Static YPoint As Short

            hDraw = RectangleHeight - StepSize

            nbm = bm.Clone

            StorageBitmap.DrawImage(nbm, New Rectangle(0, 0, RectangleWidth - StepSize, RectangleHeight), _

                                         New Rectangle(StepSize, 0, RectangleWidth - StepSize, RectangleHeight), _

                                                                     GraphicsUnit.Pixel)

            StorageBitmap.DrawLine(clearpen, RectangleWidth - 1, RectangleHeight, RectangleWidth - 1, 0) 'clear last two lines

            P0val = P0.NextValue() >> 2

            If Uni Then

                StorageBitmap.DrawLine(proc1pen, RectangleWidth - 1, RectangleHeight, RectangleWidth - 1, RectangleHeight - P0val)

            Else

                P1val = P1.NextValue() >> 2

                StorageBitmap.DrawLine(proc1pen, RectangleWidth - 2, RectangleHeight, RectangleWidth - 2, RectangleHeight - P0val)

                StorageBitmap.DrawLine(proc2pen, RectangleWidth - 1, RectangleHeight, RectangleWidth - 1, RectangleHeight - P1val)

            End If

     

            If ShowPhysMem Then

                YPoint = -CInt((My.Computer.Info.AvailablePhysicalMemory / TotPhysicalMem) * -RectangleHeight)

                StorageBitmap.DrawLine(PhysMemPen, RectangleWidth - StepSize, YPoint, RectangleWidth, YPoint)

            End If

     

            If ShowVirtMem Then

                YPoint = RectangleHeight - Math.Round(VMInUse.NextValue * Factor)

                StorageBitmap.DrawLine(VirtMemPen, RectangleWidth - StepSize, YPoint, RectangleWidth, YPoint)

            End If

            pb1.Image = bm

        End Sub

        Private Sub CtxMenu_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles CtxMenu.MouseLeave

            CtxMenu.Close()

        End Sub

        Private Sub pb1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pb1.MouseClick

            If Not My.Computer.Mouse.ButtonsSwapped Then

                If e.Button = Windows.Forms.MouseButtons.Left Then Exit Sub

            Else

                If e.Button = Windows.Forms.MouseButtons.Right Then Exit Sub

            End If

            CtxMenu.Margin = New Padding(3, 10, 3, 3)

            CtxMenu.Show()

        End Sub

        Private Sub MenuSelectionDispatcher(ByVal sender As Object, ByVal e As System.EventArgs) _

           Handles Int250.Click, Int500.Click, Int1000.Click, Int5000.Click, AvailPhysMemInUse.Click, _

           AvailVirtMemInUse.Click, MnuExit.Click

            Int250.CheckState = CheckState.Unchecked : Int500.CheckState = CheckState.Unchecked

            Int1000.CheckState = CheckState.Unchecked : Int5000.CheckState = CheckState.Unchecked

            Int250.Enabled = True : Int500.Enabled = True : Int1000.Enabled = True : Int5000.Enabled = True

            Select Case sender.name

                Case "Int250"

                    Int250.CheckState = CheckState.Checked

                    Int250.Enabled = False

                    Tmr.Interval = 250

                Case "Int500"

                    Int500.CheckState = CheckState.Checked

                    Int500.Enabled = False

                    Tmr.Interval = 500

                Case "Int1000"

                    Int1000.CheckState = CheckState.Checked

                    Int1000.Enabled = False

                    Tmr.Interval = 1000

                Case "Int5000"

                    Int5000.CheckState = CheckState.Checked

                    Int5000.Enabled = False

                    Tmr.Interval = 5000

                Case "ShowPhysMem"

                    If AvailPhysMemInUse.CheckState = CheckState.Checked Then

                        AvailPhysMemInUse.CheckState = CheckState.Unchecked

                        ShowPhysMem = False

                    Else

                        AvailPhysMemInUse.CheckState = CheckState.Checked

                        ShowPhysMem = True

                    End If

                    My.Settings.ShowPhysicalMemory = ShowPhysMem

                    My.Settings.Save()

                Case "ShowVirtMem"

                    If AvailVirtMemInUse.CheckState = Chec