0

I have some delicate code that relies on Task.Delay being accurate enough such that it does not delay less then what is expected. If it delays more, that's fine. Basically, it boils down to this:

    var now = DateTime.UtcNow;
    var then = now + TimeSpan.FromMilliseconds(10);

    await Task.Delay(10);

    if (DateTime.UtcNow < then)
    {
        // Is it possible to get here?
    }

I'm wondering if it's possible that the predicate within the if-statement can become true -- in that perhaps Task.Delay delayed slightly less, or if DateTime.Now is slightly inaccurate?

Assume that the clock will never be adjusted during execution.

Is this hardware dependent, OS dependent, or can it be guaranteed to always be false?

HelloWorld
  • 3,381
  • 5
  • 32
  • 58
  • Does this answer your question? [Accuracy of Task.Delay](https://stackoverflow.com/questions/31742521/accuracy-of-task-delay) – Igor Pashchuk Nov 17 '20 at 21:56
  • 4
    Since 10ms is less than regular timer resolution you have to carefully investigate how each of the pieces of your puzzle behave. I would not be very surprised if `.Now` can be behind enough so Now+10ms is actually less than 10ms away from "now"... (I'd stay away from such small intervals - and go for 50+ms... but still check if delay matches my expectation of "past that time") – Alexei Levenkov Nov 17 '20 at 22:03
  • `I'm wondering if it's possible that the predicate within the if-statement can become true` Yes. Read up on daylight savings time. – mjwills Nov 17 '20 at 22:21
  • @IgorPashchuk Not sure if it does. Mentions that Task.Delay depends on the resolution of the system clock (~15 ms), but unsure if DateTime.Now adheres to the same – HelloWorld Nov 17 '20 at 22:39
  • 1
    @mjwills good point. i'll rephrase to UtcNow. but your problem remains as this code will be executed on a server running in azure, which i guess is prone to clock adjustments -- as all computers are. – HelloWorld Nov 17 '20 at 22:39
  • My question to you is - why do you care? – mjwills Nov 17 '20 at 22:41
  • @mjwills because my algorithm cares. it's not enough space in the comment area to explain it, and adding it to the question will only lead to unnecessary confusion. besides, im also genuinely curious (aside from my algorithm). – HelloWorld Nov 17 '20 at 22:46
  • 1
    It is OS dependent. On Windows you may not assume that the clock is monotonous, but have a guarantee that the if-statement never is true. Both are updated from the same timing source, the clock tick interrupt. Which ticks 64 times per second by default (aka ~15 msec), but commonly ticks faster. On the many unixy flavors supported by .NETCore it gets harder to make promises, I got uncomfortable about changes without comments, but I'd be optimistic for recent versions. The "why do you care?" comment is not adequately answered on a question that makes incorrect assumptions. – Hans Passant Nov 17 '20 at 23:16

1 Answers1

1

In short, both Task.Delay and DateTime.Now are limited by the system clock precision. On Windows systems, apparently it's ~10-15 ms (see references below). Therefore, if you need better precision, I would avoid relying on something that is inherently not that precise.

References:

Igor Pashchuk
  • 2,455
  • 2
  • 22
  • 29