Why not use a synchronization method instead of waiting for an Async task?

Asynchronous Method:

public void Main(){
    await Asynctask1;
    DoSomething();
}

public async Task AsyncTask1(){
    //Async logic here
}

Sync Method:

public void Main(){
    SyncMethod1();
    DoSomething();
}

I think something is missing here. Assuming that SyncMethod1()and Asynctask1()both do the same thing, are not these two design ultimately the same result, namely, that DoSomething()does not start up until the method is finished?

+4
source share
2 answers

namely, that DoSomething () does not start until the method completes?

. await , , , , , . ( , - async/await). .

, - - -, , HTTP-. SyncMethod1 HTTP-; HTTP-, , HTTP. AsyncTask1 HTTP-; HTTP-, Task. , HTTP-, AsyncTask1 Task, await (.. DoSomething).

, : . , , ASP.NET .

async intro , , .

+4

, , , , .

, , :

public async void Main() {
    Task performTask = AsyncTask1();
    //This method can continue without having to wait for AsyncTask1 to complete
    DoSomething();
    //Here we await the AsyncTask1 because perhaps other code further 
    //down is depending on it
    await performTask;
}
0

Source: https://habr.com/ru/post/1539875/


All Articles