C# Tip: Raise synchronous events using Timer (and not a While loop)

Read Time:1 Minute, 5 Second

There may be times when you need to process a specific task on a timely basis, such as polling an endpoint to look for updates or refreshing a Refresh Token.

If you need infinite processing, you can pick two roads: the obvious one or the better one.

For instance, you can use an infinite loop and put a Sleep command to delay the execution of the next task:

There’s nothing wrong with it – but we can do better.

Introducing System.Timers.Timer

The System.Timers namespace exposes a cool object that you can use to achieve that result: Timer.

You then define the timer, choose which event(s) must be processed, and then run it:

The constructor accepts in input an interval (a double value that represents the milliseconds for the interval), whose default value is 100.

This class implements IDisposable: if you’re using it as a dependency of another component that must be Disposed, don’t forget to call Dispose on that Timer.

Note: use this only for synchronous tasks: there are other kinds of Timers that you can use for asynchronous operations, such as PeriodicTimer, which also can be stopped by canceling a CancellationToken.

This article first appeared on Code4IT 🐧

Happy coding!


Source: https://www.code4it.dev/csharptips/timer-to-trigger-synchronous-events