473,385 Members | 1,396 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 2936
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: W1ld0ne74 | last post by:
I am implimenting Performance counters into a web application. I use the following code to create the counters during setup: private void SetupPerfCntrs() {...
1
by: Chris | last post by:
I'm trying to do something really easy: just get the % CPU load. However, I just get mostly 0's sprinkled with an occasional 100. I poked around on the web for info, but all the stuff I found (I...
1
by: jimbo | last post by:
Here is my problem. I'm creating an Instrumentation class that will use previously created Performance Categories and Counters in order to time various processes (ie. query duration etc.). This...
0
by: BuddyWork | last post by:
Hello, Can someone please explain why my Instances are not appearing in Perfmon. Here is my code to create the counters. CounterCreationDataCollection CCDC = new...
0
by: Michiel | last post by:
Hello, I'm trying to retrieve my cpu load as follows : PerformanceCounter* cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total"); float value =...
0
by: Henning Krause [MVP] | last post by:
Hello, I've created a simple website which instantiates a PerformanceCounter object on any existing counter. On the Page_Load() event I set a label to the value of the performancecounter (via...
3
by: Rob Meade | last post by:
Hi all, I'm having a bit of trouble with the following function.... Private Function GetSystemUpTime() As TimeSpan ' declare variables Dim Result As TimeSpan Dim PerformanceCounter As...
0
by: Scheu | last post by:
Hi all, I'm hoping someone can help me with this bizarre problem. If I create a new PerformanceCounter object in a ComVisible class and invoke it from wscript, it never seems to terminate. When...
3
by: Geoff McElhanon | last post by:
I have been struggling with a security issue that occurs under .NET 2.0, but does not occur under .NET 1.1. Essentially I am trying to open up a performance counter on a remote server and monitor...
1
by: bobido | last post by:
Hi there! Currently I'm trying to display the current performance of my servers on my website. I tried it with the following code: protected static PerformanceCounter cpuCounter; ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.