What is the difference between async Task and void?

4

I use async Task with await when I need my code to wait for this task to complete until I run another process.

In the case of void methods, without async-await , does my code also not "wait" to execute another process until my void method is completed to run?

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 methods DoFooAsync() , called with await and DoFoo ?

    
asked by anonymous 27.07.2017 / 18:30

2 answers

4

The second example is a normal synchronous call, that is, DoFoo() is running, at some point transfers the execution stream to Foo() internally is practically a goto , when it finishes executing another goto is executed by returning to the following point where the Foo() (there are some details of how it does this control that is not the case, even because it is internal to the processor).

The first example is still two normal functions, but the form of call is quite different, it is asynchronous that 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 the methods. Not just a simple flow deviation. And there's a graph that shows how the flow becomes complex:

Notetheblackflowasitgoesbackandforthinanormalway.Theredstartsexecutionofthefunctionbutmaynotendupallowinganotherflowtorunfromthere,evenwhilethatfunctioniswaitingforexternalresponsessuchasdisk,network,servicesanddatabase.

Sothedifferenceisthattheasynchronousmethodwaitsfortheendoftheexecutionofthefunctionbutallowsanotherflowtobeexecuted,whereasthesynchronousmethodstopstherewaitingfortheprocessingtoend.

Butithasnomiracle,somehowyouhavetohaveaninfrastructurethataccessestheexternalenginegivingthechanceofanotherflowtoexecute.Ifyouareonlyconsumingsomethingyoudonothavetoworrytoomuch,butifyouarewritingexternalaccessyouneedtocoordinatethis.Here'sanexampleof how is access to asynchronous file reading , is not so trivial.

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

I'm not going to go into details about utility and functionality because there are already several answers to this:

27.07.2017 / 19:01
2

You are comparing an asynchronous call to a synchronous call.

In the case of an asynchronous call, the thread master will be released when there is a call of IO , or when waiting for the end of a CPUBound operation running on another thread . In this way, you will be releasing these resources so that they can be used by another operation, such as updating your UI.

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

    
27.07.2017 / 19:03