How does a C# Timer Object decide when an elapsed amount of time has occurred?
I am wondering if it simply loops or is more intelligent than this.
Also, it would be good to know where to find the C# Source Code to have a look through.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The .NET BCL has at least two classes providing timers:
System.Threading.TimerSystem.Timers.TimerHowever, the second class is implemented using the first. The first class uses some kind of native Win32 timer and Hans Passant has provided more information about the mechanism used. In the BCL source code the timer is implemented using an internal class named
TimerQueue. The implementation of theTimerQueueclass has this comment:So basically the AppDomain has a single time source implemented using unmanaged code. See Hans Passant’s answer for more information on that.
I use JetBrains dotPeek. It can be configured to retrieve source files from Microsoft if they exist. Simply load in the version of .NET you want to examine, hit Ctrl+T, type “Timer”, select the specific class you want and it will download the source if it exists. Otherwise you will see a decompiled version (depending on your configuration).
Note that this tool will only allow you to decomple managed code and to fully understand the implementation of timers in .NET you need to be able to peek further into unmanaged code.