Thread.Sleep(2500) vs. Task.Delay(2500).Wait()

async-await, c#, multithreading, task, task-parallel-library

Solution

Using `Wait` on an uncompleted task is indeed blocking the thread until the task completes.

Using `Thread.Sleep` is clearer since you're explicitly blocking a thread instead of implicitly blocking on a task.

The only way using `Task.Delay` is preferable is that it allows using a `CancellationToken` so you can cancel the block if you like to.

Problem

I want some clarity on this. I know that `Task.Delay` will internally use a Timer and it is obviously task-based (awaitable), whereas `Thread.Sleep` will cause the thread to be blocked. However, does calling `.Wait` on the task cause the thread to be blocked? If not, one would assume that `Task.Delay(2500).Wait()` is better than `Thread.Sleep(2500)`. This is slightly different that the SO question/answer here as I'm calling `.Wait()`.

Original source

Related problems