Click here to Skip to main content
11,804,574 members (93,545 online)
Click here to Skip to main content

Performance Monitor Made Easy!

, 10 Sep 2008 CPOL 19.1K 431 12
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!


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.


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()
   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


    'Setting up the performance counter.

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

        'Sets the baseline

        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)

           'Pauses loop for 1 second.

    Catch ex As Exception
    End Try

     End Sub

End Module

Points of interest

Hope this helps, have fun and enjoy!


  • Original post - 9-10-2008.


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


About the Author

Web Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionStop properly and start again Pin
networkmancer10-Mar-13 18:43
membernetworkmancer10-Mar-13 18:43 
GeneralThanks for the tip! Pin
meraydin29-Nov-10 21:29
membermeraydin29-Nov-10 21:29 
QuestionWhat counters are available? Pin
supercat912-Sep-08 5:43
membersupercat912-Sep-08 5:43 
AnswerRe: What counters are available? Pin
NewLaw12-Sep-08 6:20
memberNewLaw12-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 | Terms of Use | Mobile
Web04 | 2.8.151002.1 | Last Updated 10 Sep 2008
Article Copyright 2008 by NewLaw
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid