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