performancecounter

J

JvCoach23

I'm trying to write some that is database driven. What it does it loop
through a dataset and populate the

categoryName
CounterName
InstanceName
MachineName

properties of the performancecounter object (I think I have the speak right
there)

Anyway, when I run the console app, the values are always 0. For example,
the properties above have these values from the db.

with PerforanceCounter
..CategoryName = "Processor"
..CounterName = "% Processor Time"
..InstanceName = "0"
..MachineName = "."
beginInit()
end with

later in the code I do a performancecounter.nextvalue

it always comes back 0. If I step through it, the value is still 0, but if
I type in the command window, I get a value back.. like 3.34324. Can
anyone tell me what I'm doing wrong. I would like to keep this database
driven so I can add stuff easiser and not have to change the vb code.

Thanks
Shannon

(e-mail address removed)
 
I

Imran Koradia

Could you post the code that's causing the problem? somewhere around and
including where you're getting 0 as the NextValue.

Imran.
 
J

JvCoach23

For Each dr In ds.Tables(0).Rows
vcCategoryName = CType(dr.Item("vcCategoryName"), String)
vcCounterName = CType(dr.Item("vcCounterName"), String)
vcServer = CType(dr.Item("vcServer"), String)
vcInstance = CType(dr.Item("vcInstance"), String)
intTblPMInstanceId = CType(dr.Item("intTblPMInstanceId"),
Integer)

'display on screen what we are going to be going after
Console.WriteLine(vcCounterName & " - " & vcCategoryName & " -
" & vcInstance & " - " & vcServer)

'set the oPerfmon object settings
With oPerfMon
.CategoryName = vcCategoryName
.CounterName = vcCounterName
.InstanceName = vcInstance
.MachineName = vcServer
.BeginInit()
End With

Dim intVal As Single

Try
intVal = oPerfMon.NextValue


'pump that info the the screen
Console.WriteLine(intVal & " - Value in the intVal object")

'this is where it alwasy shows 0.
 
I

Imran Koradia

I'm afraid no. However, Performance counters are *generally* used to display
stats that refresh every once in a while (using a timer or something). In
that case, you know that the NextValue is going to be called multiple times
and hence you're not going to end up with a value of 0 (if its not 0). Just
a thought..Ofcourse, there are exceptions always..

Imran.
 
J

JvCoach23

Maybe your willing to give a bit more education. This works. it is on a
timer. after the first run it bring back the correct data..
Sub Main()
Dim intTimer As Integer = 3000
Console.WriteLine("Samples every " & intTimer / 1000 & " seconds.")
Console.WriteLine("Press ENTER to exit")
Dim vcServer As String
vcServer = "s011038home"
With oPerfMon
.CategoryName = "Processor"
.CounterName = "% Processor Time"
.InstanceName = "_Total"
.MachineName = vcServer
.BeginInit()
End With

CheckCounter()

tmr.Enabled = True
tmr.Interval = intTimer
Console.ReadLine()
tmr = Nothing
oPerfMon = Nothing
End Sub
Private Sub tmr_Elapsed(ByVal sender As Object, ByVal e As
System.Timers.ElapsedEventArgs) Handles tmr.Elapsed
CheckCounter()
End Sub
Private Sub CheckCounter()
Dim intVal As Integer
intVal = Math.Round(oPerfMon.NextValue)
Console.WriteLine(oPerfMon.NextValue & " - oPerfmon")
Console.WriteLine(intVal & " - intVal value")

Console.WriteLine("CPU %:" & intVal)
End Sub
However, if I move where I set the oPerfmon values.. it always returns 0.
can you explain why.
Sub Main()
Dim intTimer As Integer = 3000
Console.WriteLine("Samples every " & intTimer / 1000 & " seconds.")
Console.WriteLine("Press ENTER to exit")


CheckCounter()

tmr.Enabled = True
tmr.Interval = intTimer
Console.ReadLine()
tmr = Nothing
oPerfMon = Nothing
End Sub
Private Sub tmr_Elapsed(ByVal sender As Object, ByVal e As
System.Timers.ElapsedEventArgs) Handles tmr.Elapsed
CheckCounter()
End Sub
Private Sub CheckCounter()
Dim vcServer As String
vcServer = "s011038home"
With oPerfMon
.CategoryName = "Processor"
.CounterName = "% Processor Time"
.InstanceName = "_Total"
.MachineName = vcServer
.BeginInit()
End With

Dim intVal As Integer
intVal = Math.Round(oPerfMon.NextValue)
Console.WriteLine(oPerfMon.NextValue & " - oPerfmon")
Console.WriteLine(intVal & " - intVal value")

Console.WriteLine("CPU %:" & intVal)
End Sub

Hope your still willing to help out on this.
Thanks
Shannon
 
I

Imran Koradia

Here's what I believe is happening:
At every tick of your timer, you are setting the category name, instance
name, etc of the performance counter which means you are re-initializing the
performance counter. When that happens and if your performance counter is
one which would require two reads to get the correct value, then the first
value read with the NextValue method will be 0. So now what's happening is
that everytime your elapsed event is fired, you are simply re-initializing
the counter and wiping out whatever previous values it had which is why
you're always getting 0. What you did in the first part is correct. You
initialize the counter somewhere, then fire the timer and within the elapsed
event, keep calling NextValue to get the performance counter values. If you
need to keep track of several performance counter values, either define
different peformance counter objects for different counters or define a
collection (array, arraylist, hashtable, etc whichever is suitable) of
performance counters which holds one object for each performance counter you
want to keep track of.

hope that helps..
Imran.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top