private int TicklessTimer()
{
// Calculate tickless timer,
// by adding the timeout to the current point-in-time.
long tickless = Clock.NowMs() + PollTimeout;
// Find the When-value of the earliest timer..
foreach (var timer in m_timers)
{
// If it is enabled but no When is set yet,
if (timer.When == -1 && timer.Enable)
{
// Set this timer's When to now plus it's Interval.
timer.When = timer.Interval + Clock.NowMs();
}
// If it has a When and that is earlier than the earliest found thus far,
if (timer.When != -1 && tickless > timer.When)
{
// save that value.
tickless = timer.When;
}
}
// Compute a timeout value - how many milliseconds from now that that earliest-timer will expire.
var timeout = (int)(tickless - Clock.NowMs());
// Use zero to indicate it has already expired.
if (timeout < 0)
{
timeout = 0;
}
// Return that timeout value.
return timeout;
}