Thread.Sleep Waking Up Early

  • Thread starter Thread starter O.B.
  • Start date Start date
O

O.B.

In my program, I have a Thread.Sleep(32000). I have some DateTime.Now
()'s around this statement. I am running my program with NUnit.
Every now and then, the thread wakes after only sleeping 24 seconds.
No exceptions are being thrown. Anyone have a clue what's going on
here?
 
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;
}
}
}
 
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;
}
}

}

What happens if you use a Stopwatch instance instead of a DateTime?

Chris
 
O.B. said:
milliseconds = (int) (stopTime -DateTime.UtcNow).TotalMilliseconds;

The line above does not really do anything useful. Did you forget to clip
it, or did you think it was changing the passed parameter?
 
Back
Top