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

Alternate to Thread.Sleep

4.50/5 (4 votes)
27 Apr 2011CPOL 34.1K  
Thread.Sleep is actually used to block the thread Temporarily for an interval. Async CTP brings forth one probable alternative if you dont want to write yourself : TaskEx.Delay(milliseconds)Probably in next release it will be Task.Delay(milliseconds)This works little different as it...
Thread.Sleep is actually used to block the thread Temporarily for an interval. Async CTP brings forth one probable alternative if you dont want to write yourself :

TaskEx.Delay(milliseconds)


Probably in next release it will be Task.Delay(milliseconds)

This works little different as it can work with await, but you can easily put a Task.Wait to get similar result as Thread.Sleep.

Task t = TaskEx.Delay(500);
t.Wait()


is same as

Thread.Sleep(500)


Check my whole article
C# 5.0 vNext - New Asynchronous Pattern[^]

License

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