To my astonishment, I have found that .NET Task Library does not (seem to) have support for executing a task on timer, similar to Observable.Interval(). I needed to execute an activity every second in my async method, and the suggestions I found on StackOverflow were quite awful: from using Task.Delay() to using a dedicated thread and Thread.Sleep().

Task.Delay() is good enough in many cases, but it has a problem. Consider this code:

One execution of the loop takes 1300ms on average, instead of 1000ms we wanted. So, on every execution we will accumulate an additional delay of 300ms. If DoSomething() takes different time from iteration to iteration, our activity will not only be late, but it will also be irregular. One could, of course, use real-time clock to adjust the delay after every execution, but this is quite laborious and hard to encapsulate.

Using TaskTimer

Browser for “TaskTimer” package on NuGet and add it to your project. Alternatively, you can get the code from GitHub. The code requires .NET 4.5 or above.

You will need to choose the timer period, and the initial delay, or absolute start time:

The returned object is a disposable enumberable. You must call Dispose() on it to properly disposer of the underlying timer object. The enumerable iterates over an infinite series of tasks. Task number N becomes completed after the timer ticks N times. The enumerable can be enumerated only once.

Avoiding infinite loop

The enumerable returned by the timer is infinite, so you cannot get all the task at once.

The trouble with this approach was that calling the Start() method did not actually start the timer: the timer would be started only when one begins to iterate over it. Furthermore, each iteration will create a new timer. I found this too confusing. I had enough trouble understanding similar behavior in observables to inflict this on the users of my own class.

So, I decided to switch to a schema where the timer gets started immediately upon calling of the Start() method. In reactive terms, TaskTimer is a hot observable. To stop the timer, one needs to dispose the return value of the Start() method, hence the disposable enumerable. To use the timer properly, one should put a foreach loop inside a using block like in the examples, above.

Also, it is only possible to iterate over the timer once. The following will not work:

It should be possible to implement a timer with multiple iteration semantics, but I decided it was not worth the complexity.

Conclusion

It’s a shame Task Library does not provide timer object out of the box. I hope I was able to fill the gap, at least to some extent. In the process I ran into interesting problems with the way IEnumerable, IEnumerator and IDisposable intersect, this is a fascinating world. I hope you do enjoy the result!