Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the data type of System.Timers.Timer.Interval a double?

Tags:

c#

.net

timer

This is a bit of an academic question as I'm struggling with the thinking behind Microsoft using double as the data type for the Interval property!

Firstly from MDSN Interval is the time, in milliseconds, between Elapsed events; I would interpret that to be a discrete number so why the use of a double? surely int or long makes greater sense!?

Can Interval support values like 5.768585 (5.768585 ms)? Especially when one considers System.Timers.Timer to have nowhere near sub millisecond accuracy... Most accurate timer in .NET?

Seems a bit daft to me.. Maybe I'm missing something!

like image 947
Dave Lawrence Avatar asked Jul 23 '12 10:07

Dave Lawrence


People also ask

What is the use of system timer?

Overview. Timers are peripheral devices that can be used to set the interval between events. In embedded systems design, the ability to precisely set the timing between events is extremely common. Timers are used to in multi-threaded operating systems to determine how long a task is active before swapping to a new task ...

What is interval in timer C#?

The key terms when using the timer class are: Interval: Time interval between two successive invocations of Elapsed function. Callback: The timer invokes this method at the end. AutoReset: Boolean that determines whether the timer will raise the Tick event each time the specified interval has elapsed.

What is timer interval in VB net?

The time, in milliseconds, between Elapsed events. The value must be greater than zero, and less than or equal to Int32. MaxValue. The default is 100 milliseconds.

What is timer AutoReset?

Timer AutoReset Gets or sets a value indicating whether the Timer should raise the Elapsed event each time the specified interval elapses or only after the first time it elapses.


2 Answers

Disassembling shows that the interval is consumed via a call to (int)Math.Ceiling(this.interval) so even if you were to specify a real number, it would be turned into an int before use. This happens in a method called UpdateTimer.

Why? No idea, perhaps the spec said that double was required at one point and that changed? The end result is that double is not strictly required, because it is eventually converted to an int and cannot be larger than Int32.MaxValue according to the docs anyway.

Yes, the timer can "support" real numbers, it just doesn't tell you that it silently changed them. You can initialise and run the timer with 100.5d, it turns it into 101.

And yes, it is all a bit daft: 4 wasted bytes, potential implicit casting, conversion calls, explicit casting, all needless if they'd just used int.

like image 154
Adam Houldsworth Avatar answered Sep 17 '22 15:09

Adam Houldsworth


The reason to use a double here is the attempt to provide enough accuracy.

In detail: The systems interrupt time slices are given by ActualResolution which is returned by NtQueryTimerResolution(). NtQueryTimerResolution is exported by the native Windows NT library NTDLL.DLL. The System time increments are given by TimeIncrement which is returned by GetSystemTimeAdjustment().

These two values are determining the behavior of the system timers. They are integer values and the express 100 ns units. However, this is already insufficient for certain hardware today. On some systems ActualResolution is returned 9766 which would correspond to 0.9766 ms. But in fact these systems are operating at 1024 interrupts per second (tuned by proper setting of the multimedia interface). 1024 interrupts a second will cause the interrupt period to be 0.9765625 ms. This is of too high detail, it reaches into the 100 ps regime and can therefore not be hold in the standard ActualResolution format.

Therefore it has been decided to put such time-parameters into double. But: This does not mean that all of the posible values are supported/used. The granularity given by TimeIncrement will persist, no matter what.

When dealing with timers it is always advisable to look at the granularity of the parameters involved.

So back to your question: Can Interval support values like 5.768585 (ms) ?

No, the system I've taken as an example above cannot.

But it can support 5.859375 (ms)!

Other systems with different hardware may support other numbers.

So the idea of introducing a double here is not such a stupid idea and actually makes sense. Spending another 4 bytes to get things finally right is a good investment.

I've summarized some more details about Windows time matters here.

like image 33
Arno Avatar answered Sep 18 '22 15:09

Arno