Geeks With Blogs
Nouman Zakir while (true) { Post.NewArticle(); }
Environment.TickCount returns a 32-bit signed integer containing the amount of time in milliseconds that has passed since the last time the computer was started. But this value can overflow for systems that stay up for days at a time. To avoid this overflow problem, you can query the "System Up Time" performance counter:

public TimeSpan SystemUpTime()

PerformanceCounter upTime = new PerformanceCounter("System", "System Up Time");

// You've got to call this twice. First time it returns 0 and the second time it returns the real info.


return TimeSpan.FromSeconds(upTime.NextValue());
You need proper privileges to query performance counter.
Posted on Sunday, January 18, 2009 6:13 AM | Back to top

Comments on this post: Performance counter without overflow

# re: Performance counter without overflow
Requesting Gravatar...
This only returns the uptime since last boot OR system resume. You need to dig deeper to find the 'real' original start-up time.

Left by Jim on Feb 25, 2009 4:01 PM

# re: Performance counter without overflow
Requesting Gravatar...
I have been searching for a better explanation about this topic and I think I got it right from this blog. I am very happy in that and I appreciate you for sharing this useful stuff. Thank you very much for sharing this post! hotels catalina island
Left by Roy on Sep 14, 2015 9:18 AM

Your comment:
 (will show your gravatar)

Copyright © Nouman Zakir | Powered by: