简体   繁体   中英

C# Reusing timers vs Allocating new one

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

PS: I'm talking about System.Threading.Timer class

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM