You didn't post a concise-but-complete code sample that reliably
demonstrates the problem so, no...no clue.
An eight-second difference points to either a bug in your own code (i.e.
you're not measuring what you think you're measuring) or some sort of
lower-level hardware issue (i.e. something else going on in the system
that causes the system clock to be inaccurate). Since you are reporting
an error of 33%, and since that degree of inaccuracy would show up in all
sorts of other things if it was going on for extended periods of time on
your system as you're suggesting, I'm inclined to suspect the former
rather than the latter.
But without a reproducible scenario, it's not possible to suggest what the
issue might be.
Pete
Thanks Pete. The problem is not consistently reproducible. At first
I though the thread might be getting interrupted, but that would come
across as a ThreadInterruptedException. Since no exceptions are being
thrown, this isn't the case. I have abstracted the odd behavior into
the operation below. The if statement within the while loop is what
is getting invoked from time to time. The MSDN absolutely sucks for
the Thread.Sleep operation; so I've been trying to glean some useful
information about this from people's blogs but haven't come up with
anything yet.
public static void ThreadSleep(int milliseconds)
{
DateTime stopTime = DateTime.UtcNow.AddMilliseconds(milliseconds);
while (true)
{
Thread.Sleep(milliseconds);
if (DateTime.UtcNow < stopTime)
{
Console.WriteLine("\n***** SLEEP THREAD WOKE PREMATURELY: " +
DateTime.UtcNow + " instead of " +
stopTime +
" *****\n");
milliseconds = (int) (stopTime -
DateTime.UtcNow).TotalMilliseconds;
}
else
{
break;
}
}
}