Joe Enzminger
Joe Enzminger

Reputation: 11190

Does System.Threading.Timer consume a thread while waiting?

If I start a System.Threading.Timer, does it consume a ThreadPool thread while waiting (bad), or only when the timer is executing the TimerCallback?

Upvotes: 1

Views: 213

Answers (2)

Hans Passant
Hans Passant

Reputation: 941327

Yes. And no. It depends on your .NET framework version. Traditionally no, it consumed an entire thread. Started by the CLR to handle all active timers.

Not so on more recent versions, like my 4.5.1 running on Windows 8.1. It has switched to using CreateTimerQueueTimer to get that job done. That's pooled by the operating system.

Get insight by enabling unmanaged debugging and setting a breakpoint on the timer's callback. Look at the call stack.

Upvotes: 3

Jon
Jon

Reputation: 437336

Only when the callback is being executed. Note that if the duration of the callback is longer than the timer interval multiple thread pool threads will end up executing the callback concurrently.

Upvotes: 2

Related Questions