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 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
> 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
Anyone know if its possible to pull the same information from different
machines on the network?

A new instance of PerformanceCounter seems to be able to take a machine
name - but I've tried this and get access denied errors...

I guess i some how need to be able to tell the servers in question that the
ASP.Net process model is allowed to access this?

Any ideas anyone?

My other thought was as a web service, so I have one page which drags the
info from the other servers, this would of course then rely on IIS being
present, not so good for my clustered SQL Servers! Open to suggestions...

Regards

Rob

0 comments:

Post a Comment