Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Alternate to Thread.Sleep

1.02/5 (31 votes)
27 Apr 2011CPOL 29.7K  
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)
  {;}
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)