S
S
sergeevpetro2016-09-20 10:03:32
C++ / C#
sergeevpetro, 2016-09-20 10:03:32

Async-await and Task.wait() What's the difference?

Good day!
As I understand it, await is waiting for the completion of the operation and wait () is doing the same.
So what's the difference? Why await if there is a wait()? Why wait() if there is await?
Explain in plain language please)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey Kulakov, 2016-09-20
@carbon88

Task.Wait() will block the calling thread, while await will not.
The so-called continuation (whatever is below await) will be executed after the completion of the task, which is expected through await. In this case, the calling thread will not be blocked and will be able to do some other work, for example, respond to a button click if this is the main thread of the GUI application or if this is an ASP.no application, it will be able to start processing the next request.
As soon as the task is completed, the execution of the continuation will begin and in some cases it is not guaranteed that this will be the same thread in which the code was executed before await.

R
Roman, 2016-09-20
@yarosroman

Wait it's old. async\await is just syntactic sugar. When using async\await, all exceptions are thrown into the calling thread, and you can wrap everything in a try/catch, of course, code readability, and so on. Here is a good article https://habrahabr.ru/post/260217/ and as an option there is a book Asynchronous Programming in C# 5.0 by Alex Davis.

S
Sergey, 2016-09-20
@senal

Read the difference between async (async\await) and multithreading (Task.wait())

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question