Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Reusing timers vs Allocating new one

Tags:

Should I reuse timers or allocating new one on demand? Do many sleeping timers timer.Change(Timeout.Infinite, Timeout.Infinite) consumes resources?

P.S: I'm talking about System.Threading.Timer class

like image 729
moien Avatar asked Dec 09 '18 15:12

moien


1 Answers

No, in the current implementation of the .NET framework, timers that are changed to infinite timeout do not consume system ressources (except the memory used to store the instance itself), because they are actually deleted from the internal timer queue:

if (dueTime == Timeout.UnsignedInfinite)
{
    TimerQueue.Instance.DeleteTimer(this);
    success = true;
}

quoted from reference source

like image 68
Cee McSharpface Avatar answered Nov 15 '22 05:11

Cee McSharpface