It never returns because the task was never started, so it hangs. Call the Task.Start
method:
Task t = new Task(() => Thread.Sleep(2000));
t.Start();
Or use Task.Run
instead:
Task t = Task.Run(() => Thread.Sleep(2000));
Or use Task.Delay
instead which is non-blocking (unless you used Thread.Sleep
just for the sake of an example):
Task t = Task.Delay(2000);
Bear in mind that in a web application, everything except I/O-bound work should be synchronous. Deferring CPU-bound work to the thread pool won't buy you anything - if anything, it'll hurt performance.
See Stephen Cleary's Task.Run Etiquette Examples: Don't Use Task.Run in the Implementation