1

Hans and I had small discussion recently about the subject and I'm curious how it is really implemented.

See initial talking in the comments here: Are c# timers naturally multithreaded?

Community
  • 1
  • 1
Ivan Danilov
  • 14,287
  • 6
  • 48
  • 66
  • 1
    Downvote because looking at this question and answers alone doesn't offer any information at all. I have to go look at the other thread to get any input from "Hans" who you reference in the accepted answer and I have to go to your blog to get any details about how it works. – Thymine Apr 25 '12 at 19:30

2 Answers2

2

Staring at the .Net 4.0 reference source, System.Timers.Timer seems to use a System.Threading.Timer to handle the actual implementation details. The latter generates timers by calling AddTimerNative. AddTimerNative is an internal call.

Googling AddTimerNative lead to a lot of interesting results, including Willy Denoyette's comment, where he states that it calls CreateTimerQueueTimer.

That page states that "Callback functions are queued to the thread pool. "

Brian
  • 25,523
  • 18
  • 82
  • 173
2

Well, I did some investigations... Details are in my blog post

To be short, just summary from there:

  • As for SSCLI20 Hans was totally right. There’s really separate thread for handing APCs and queued timers. Thanks for your insistence, btw. I received a chance to dig something interesting :)
  • On newer systems it still could be implemented without additional threads. I was just mistakenly assuming it was already there when CLR 2.0 was written. For details see CreateThreadpoolTimer, SetThreadpoolTimer and CloseThreadpoolTimer.
Ivan Danilov
  • 14,287
  • 6
  • 48
  • 66