Where’d the Timer Go?

So. System.Threading.Timer. Right.

I’m really starting to miss the old System.Timers.Timer. Built like a tank. System.Threading.Timer is a bit more finicky, but is the only option in .Net Core.

My fun of the day:

It turns out, when the timer goes out of scope, it will be disposed of. Even if you’d told it to, for example, tick every 3 seconds. Though I probably should have known that.

This will tick exactly once:

private void foo()
{
Timer t = new Timer(bar, null, 0, 3000);
}

Instead, you must do:

Timer t = null;
private void foo()
{
t = new Timer(bar, null, 0, 3000);
}

Just so you don’t waste a couple of frustrating hours like I did.

Happy coding!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.