Implementing Timers

It would be easy to use existing C# constructs, such as sleeping threads or for and while loops, to control the periodic collection of performance counter data. The primary problems with these methods are their synchronous nature. Furthermore, loops like for and while deliver a significant performance hit. A better solution for performing logic via specified intervals is the timer.

A timer can be set for a specified time interval, executing a callback routine whenever that interval elapses. The primary benefit of this approach is the asynchronous behavior of the timer, which delivers much better performance than the synchronous methods discussed earlier. Just set the timer interval, assign a callback routine to execute, and ...

Get C# Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.