469,282 Members | 2,021 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

performancecounter

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

ra*****@sauder.com
Nov 21 '05 #1
7 2779
Could you post the code that's causing the problem? somewhere around and
including where you're getting 0 as the NextValue.

Imran.

"JvCoach23" <ra*****@sauder.com> wrote in message
news:1d*******************************@40tude.net. ..
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

ra*****@sauder.com

Nov 21 '05 #2
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.
On Fri, 24 Sep 2004 16:36:58 -0400, Imran Koradia wrote:
Could you post the code that's causing the problem? somewhere around and
including where you're getting 0 as the NextValue.

Imran.

"JvCoach23" <ra*****@sauder.com> wrote in message
news:1d*******************************@40tude.net. ..
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

ra*****@sauder.com

Nov 21 '05 #3
Maybe this applies in your case:
http://msdn.microsoft.com/library/de...valuetopic.asp

<quote>
If the calculated value of a counter depends on two counter reads, the first
read returns 0.0.
</quote>

If the value turns out to be zero, you can call NextValue again and check
the value.

hope that helps..
Imran.

"JvCoach23" <ra*****@sauder.com> wrote in message
news:vy****************************@40tude.net...
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.
On Fri, 24 Sep 2004 16:36:58 -0400, Imran Koradia wrote:
Could you post the code that's causing the problem? somewhere around and
including where you're getting 0 as the NextValue.

Imran.

"JvCoach23" <ra*****@sauder.com> wrote in message
news:1d*******************************@40tude.net. ..
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

ra*****@sauder.com

Nov 21 '05 #4
On Mon, 27 Sep 2004 22:50:06 -0400, Imran Koradia wrote:
http://msdn.microsoft.com/library/de...valuetopic.asp


great.. thanks for the help.. I had figured that out with testing, but am
new to vb.net, doubted myself and figured I was doing something wrong. do
you happen to have a list of which counters need to be run twice..

no biggie.. I appreciate the help
Nov 21 '05 #5
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.

"JvCoach23" <ra*****@sauder.com> wrote in message
news:1e****************************@40tude.net...
On Mon, 27 Sep 2004 22:50:06 -0400, Imran Koradia wrote:
http://msdn.microsoft.com/library/de...valuetopic.asp
great.. thanks for the help.. I had figured that out with testing, but am
new to vb.net, doubted myself and figured I was doing something wrong. do
you happen to have a list of which counters need to be run twice..

no biggie.. I appreciate the help

Nov 21 '05 #6
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
Nov 21 '05 #7
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.

"JvCoach23" <ra*****@sauder.com> wrote in message
news:1i******************************@40tude.net.. .
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

Nov 21 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by BuddyWork | last post: by
reply views Thread by Michiel | last post: by
reply views Thread by Henning Krause [MVP] | last post: by
3 posts views Thread by Rob Meade | last post: by
1 post views Thread by bobido | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.