What is the difference between async Task and void?

Asked

Viewed 2,000 times

5

I use async Task with await when I need my code to wait for such a task to complete until it performs another process.

In the case of methods void, without async-await, my code also does not "wait" to perform another process until my method void be completed for execution?

async Task DoFooAsync()
{
    await FooAsync();
    // ...
}

async Task FooAsync()
{
    // minha operação de execução longa
}

void DoFoo()
{
    Foo();
    // ...
}

void Foo()
{
    // minha operação de execução longa
}

In the example, what is the difference in the execution of the methods DoFooAsync(), called with await and DoFoo?

  • 1

    Hello Vinicius. If you have patience, give a read in that reply I gave the other question. I tried to create an analogy to explain the queue of requests that the server receives and meets, with and without using the async/await. Was an example half-bare, but I think I can get the idea.

2 answers

6


The second example is a normal call synchronous, that is to say, DoFoo() is running, at a given time transfers the execution flow to Foo(), internally is practically a goto, when this one finishes executing another goto runs back to the next point where the Foo() (there are some details of how it does this control that is not the case, even for being internal to the processor).

The first example is still two normal functions, but the calling form is quite different, it is asynchronous which allows the execution to be released to perform other things.

There is a complex infrastructure mounted by the C# compiler to control a state machine that controls the execution of methods. Not just a simple flow deviation. And there’s a graph that shows how the flow becomes complex:

Fluxo de métodos assíncronos

Note the black flow as it goes back and forth normally. Red starts running the function but may not end up allowing another stream to run from there, even while that function is waiting for external responses like disk, network, services and database.

So the difference is that the asynchronous method waits for the end of the execution of the function but allows another flow to be executed, while the synchronous method stands there waiting for the end of the processing.

But there is no miracle, somehow it is necessary to have an infrastructure that accesses the external mechanism giving the chance of another flow run. If you are only consuming something you don’t have to worry too much, but if you are writing external access you need to coordinate it. See an example of how is access to read asynchronous file (no . NET Core), is not so trivial.

There has always been the asynchronous form in the . NET, but it was too complicated to consume because you had to write the code to coordinate the flow, now the compiler writes for you.

I will not go into detail about the usefulness and functioning because there are already several answers to this:

4

You’re comparing an asynchronous call to a synchronous call.

In the case of asynchronous call, thread will be released when there is any call from IO, or when waiting for the end of an operation CPUBound being executed in another thread. This way, you will be releasing these resources to be used by another operation, such as updating your UI.

In the case of synchronous call, they will be blocked until the end of the execution, making the system have to allocate more resources to execute other processes, or have to wait until the end of the current process.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.