Monday, March 26, 2012

PerformanceCounter / System Up Time / TimeSpan

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 PerformanceCounter
' create new instances of our objects
PerformanceCounter = New PerformanceCounter("System", "System Up Time")
' populate
Result = TimeSpan.FromSeconds(PerformanceCounter.NextValue())
' return result
Return Result
End Function
For some reason I keep getting the value 0 (zero) for days, hours,minutes,
seconds etc - ie, I've tried this on 2 machines now and the result is always
the same. I've tried other counters using a similar method (for available
memory,application restarts etc) and they do come through...
Can anyone shed any light on this for me...
PS: I've had a bit of a look online and I'm aware of the environment.tick
thing but that has a problem over a value of 24 (goes to minus numbers) so
would prefer to use a PerformanceCounter object.
Regards
RobThe first call always returns zero, apparently... see the page here:
http://www.sitepoint.com/forums/pri...ad.php?t=376778
Regards,
Brett.
Rob Meade wrote:
> 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 PerformanceCounter
> ' create new instances of our objects
> PerformanceCounter = New PerformanceCounter("System", "System Up Time"
)
> ' populate
> Result = TimeSpan.FromSeconds(PerformanceCounter.NextValue())
> ' return result
> Return Result
> End Function
> For some reason I keep getting the value 0 (zero) for days, hours,minutes,
> seconds etc - ie, I've tried this on 2 machines now and the result is alwa
ys
> the same. I've tried other counters using a similar method (for available
> memory,application restarts etc) and they do come through...
> Can anyone shed any light on this for me...
> PS: I've had a bit of a look online and I'm aware of the environment.tick
> thing but that has a problem over a value of 24 (goes to minus numbers) so
> would prefer to use a PerformanceCounter object.
> Regards
> Rob
"Brett Wiltshire" wrote ...

> The first call always returns zero, apparently... see the page here:
> http://www.sitepoint.com/forums/pri...ad.php?t=376778
Hi Bretty,
Thanks for your reply - you know - in the example I saw that had this code
it actually said about that - I assumed - clearly wrongly - that the
"NextValue()" thing meant - go get the next one because the first one is
always zero...
*sigh*
That's taken up 2 hours of my day searching the net just for a
misunderstanding :o(
Anyway, many thanks for your reply - I've for now just duplicated the line
that gets it and it works fine :o)
Thanks
Rob

0 comments:

Post a Comment