Nouman Zakir

while (true) { Post.NewArticle(); }
posts - 13, comments - 12, trackbacks - 0

My Links

News

Archives

Post Categories

Personal Website

Sunday, January 18, 2009

Performance counter without overflow

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.

upTime.NextValue();

return TimeSpan.FromSeconds(upTime.NextValue());
}
 
You need proper privileges to query performance counter.

posted @ Sunday, January 18, 2009 6:13 AM | Feedback (1) |

Powered by: