Click here to Skip to main content
15,860,972 members
Articles / Programming Languages / Visual Basic
Article

Performance Monitor Made Easy!

Rate me:
Please Sign up or sign in to vote.
3.43/5 (9 votes)
10 Sep 2008CPOL1 min read 31.8K   599   13   4
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.

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


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionStop properly and start again Pin
networkmancer10-Mar-13 18:43
networkmancer10-Mar-13 18:43 
GeneralThanks for the tip! Pin
meraydin29-Nov-10 21:29
meraydin29-Nov-10 21:29 
QuestionWhat counters are available? Pin
supercat912-Sep-08 5:43
supercat912-Sep-08 5:43 
AnswerRe: What counters are available? Pin
NewLaw12-Sep-08 6:20
NewLaw12-Sep-08 6:20 

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

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