await vs Task.Wait - Deadlock?

后端 未结 3 1664
生来不讨喜
生来不讨喜 2020-11-21 07:09

I don\'t quite understand the difference between Task.Wait and await.

I have something similar to the following functions in a ASP.NET WebA

3条回答
  •  生来不讨喜
    2020-11-21 07:15

    Based on what I read from different sources:

    An await expression does not block the thread on which it is executing. Instead, it causes the compiler to sign up the rest of the async method as a continuation on the awaited task. Control then returns to the caller of the async method. When the task completes, it invokes its continuation, and execution of the async method resumes where it left off.

    To wait for a single task to complete, you can call its Task.Wait method. A call to the Wait method blocks the calling thread until the single class instance has completed execution. The parameterless Wait() method is used to wait unconditionally until a task completes. The task simulates work by calling the Thread.Sleep method to sleep for two seconds.

    This article is also a good read.

提交回复
热议问题