Performance Monitor Made Easy!
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.