Click here to Skip to main content
Click here to Skip to main content

Performance Monitor Made Easy!

, 10 Sep 2008
Rate this:
Please Sign up or sign in to vote.
Using .NET Diagnostics to extract data from any Windows performance monitor, to make your own!

Introduction

This is a simple way to retrieve any performance information from your PC using the .NET framework diagnostics. This demo provides an example of how to retrieve the current % CPU usage on your local computer and output the results to a console window.

Background

This took me a while to figure out, as you may find there are not many to-the-point articles or examples regarding performance monitors. I found many articles describing how to make custom ones but I need to extract the information and store the results.

One use of this code is to perform email alerts to admin persons about processor utilization, memory utilization, etc. (any performance metric) which fall outside a set of parameters indicating something may be going wrong on the machine. Also, by gathering and storing this information over time, you can track your trends on your PC. There is a complementary article I will try to post soon which gives you the code to display the CPU usage/memory usage for all the processes on the PC such as you see in the Task Manager.

Using the code

Start a new Console project (VB.NET) and use the code below.

Module SimplePerformanceMonitor
  'This sets up a global variable thread
   Public TH1 As New _
     System.Threading.Thread(AddressOf PerformancMonitor)

   'Sub Main is the starting point 
   'of the application and starts the thread.

   Sub Main()
       TH1.Start()
   End Sub
   
   Public Sub PerformancMonitor()
    Dim bForever As Boolean = False
    Dim iFrequency As Integer = 1000
    Dim pCounter As System.Diagnostics.PerformanceCounter

'*******************************************************************

'***These variables can be interchanged depending
'   on which performance object to watch. ***

'***Open the windows performance monitor, right click
'   and add counter to see a list of options.***

'*******************************************************************

    'This is the Perfomance Object. 
    Dim sCategory As String = "Processor"
    'This represents the counter 
    Dim sCounter As String = "% Processor Time"
    'This represents the instance of the counter
    Dim sInstance As String = "_Total"
    Dim Sample As System.Diagnostics.CounterSample

    Try

    'Setting up the performance counter.

        pCounter = New PerformanceCounter
        pCounter.CategoryName = sCategory
        pCounter.CounterName = sCounter
        pCounter.InstanceName = sInstance

        'Sets the baseline

        pCounter.NextValue()
        Sample = pCounter.NextSample
        'This is an endless loop controlled by the thread TH1

        Do While bForever = False

           'Retrieves the calculated data.
           Dim nv As Single = pCounter.NextValue()
           'Retrieves the Raw data
           Dim Sample2 As CounterSample = pCounter.NextSample()
           'Keeps rolling avg
           Dim avg As Single = CounterSample.Calculate(Sample, Sample2)
           Console.WriteLine("% CPU = " & nv.ToString & " -- " & _
                             "Avg % CPU = " & avg.ToString & _
                             " , " & Date.Now.ToString)

           Threading.Thread.Sleep(1000)
           'Pauses loop for 1 second.
        Loop

    Catch ex As Exception
        Console.WriteLine(ex.Message)
    End Try

     End Sub

End Module

Points of interest

Hope this helps, have fun and enjoy!

History

  • Original post - 9-10-2008.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

NewLaw
Web Developer
United States United States
No Biography provided

Comments and Discussions

 
QuestionStop properly and start again Pinmembernetworkmancer10-Mar-13 18:43 
GeneralThanks for the tip! Pinmembermeraydin29-Nov-10 21:29 
QuestionWhat counters are available? Pinmembersupercat912-Sep-08 5:43 
AnswerRe: What counters are available? PinmemberNewLaw12-Sep-08 6:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 10 Sep 2008
Article Copyright 2008 by NewLaw
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid