Best way to disable a System.Threading.Timer

.net, timer

Solution

Setting a timer's initial value and interval to -1 (i.e. `Change(-1, -1)`) is not a "CPU heater." Timers don't use CPU resources. The callback method does, of course, but only during the (usually brief) time when it's executing.

In any case, it is not at all expensive to create a new timer, nor will disabling it with `Change(-1, -1)` have any negative performance consequences. Use whichever technique best fits your model.

Problem

I have a `System.Threading.Timer` that will be toggled on and off. I know of two methods to turn off the timer: - Call `Timer.Change(-1,-1)` - Dispose the timer, and create a new one when needed Which one is better, in terms of resources and performance? Is calling `Change(-1,-1)` a CPU heater? Is it expensive to create a timer?

Original source

Related problems