29

This question already has an answer here:

Quick question..

In order to get some solid base understanding about Asynchronous Programming and the await I would like to know what is the difference between these two code snippets when it comes to multi threading and the execution sequence and time:

This:

public Task CloseApp()
{
        return Task.Run(
                         ()=>{ 
                                // save database
                                // turn off some lights
                                // shutdown application
                          });
}

Versus this:

public async Task CloseApp()
{
        await Task.Run(
                         ()=>{ 
                                // save database
                                // turn off some lights
                                // shutdown application
                          });
}

if I am calling it in this routine:

private async void closeButtonTask()
{
    // Some Task 1
    // ..

    await CloseApp();

    // Some Task 2
    // ..
}


2 답변


31

It is almost the same (in terms of threads etc.). But for the second one (using await) a lot more overhead will be created by the compiler.

Methods declared as async and using await are converted into a state machine by the compiler. So when you hit the await, the control flow is returned to the calling method and execution of your async method is resumed after the await when the awaited Task has finished.

As there is no more code after your await, there is no need to use await anyway. Simply return the Task is enough.


  • "when you hit the await, the control flow is returned to the calling method" -- The control flow might be returned to the calling method, particularly it won't return when the awaited task is already complete. - acelent
  • @acelent thx for the detail, never thought about what happens when the task is already finished. I don't know the exact details of what the compiler does line by line. - René Vogt
  • I totally dont understand this - monstro
  • When your task will be finished a thread from Thread Pool will call the Post method and notify the caller that the task was finished. As I remember the rest of async method will be executed as ContinueWith. Execution will continue from await point, but it can be executed in different thread. - Joseph Katzman

5

There are very few differences between the two approaches. Basically, they share the same semantics. However, the version with async/await wraps the execution of the inner task in an outer compiler-generated task. The non-async version does not. Thus, the non-async version is (very marginally) more efficient.


  • "the non-async version is (very marginally) more efficient." -- That depends, if the method is called very often, then the async/await version might generate noticeable garbage: the state machine and another task. - acelent
  • @acelent: generating some generation 0 garbage still feels pretty marginal to me - Falanwe
  • Ok, I just want to point out that is not always the case for everyone (1), and you can't rely on tasks or references to tasks to be that ephemeral (2, at 0:25:40, at 0:30:20). - acelent

Linked


Related

Latest