65.9K
CodeProject is changing. Read more.
Home

Alternate to Thread.Sleep

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.02/5 (30 votes)

Apr 20, 2011

CPOL
viewsIcon

29790

I coded this small stub to waste time as an alternate to Thread.Sleep:
private void WasteTime(TimeSpan unit)
{
  DateTime now = DateTime.Now;
  DateTime then = DateTime.Now + unit;

  while (now < then)
  {
    now = DateTime.Now;
  }
}
Works quite preceisely.
private void WasteTime(TimeSpan unit)
{
  DateTime then = DateTime.Now + unit;
  while (DateTime.Now < then)
  {;}
}