Multiple await operations or just one

Umair

I've seen how the await keyword is implemented and resulting structure it creates. I think I have a rudimentary understanding of it. However, is

public async Task DoWork()
{
    await this.Operation1Async();
    await this.Operation2Async();
    await this.Operation3Async();
}

"better" (generally speaking) or

public async Task DoWork()
{
    await this.Operation1Async();
    this.Operation2();
    this.Operation3();
}

The problem with the first approach is that it is creating a new Task for each await call? Which entails a new thread?

Whereas the first creates a new Task on the first await and then everything from there is processed in the new Task?

Edit Ok maybe I wasn't too clear, but if for example we have

while (await reader.ReadAsync())
{
    //...
}

await reader.NextResultAsync();

// ...

Is this not creating two tasks? One in the main thread with the first ReadAsync then another task in this newly created task with the NextResultAsync. My question is there really a need for the second task, isn't the one task created in the main thread sufficient? So

while (await reader.ReadAsync())
{
    //...
}

reader.NextResult();

// ...
Stephen Cleary

it is creating a new Task for each await call? Which entails a new thread?

Yes and no. Yes, it is creating a Task for each asynchronous method; the async state machine will create one. However, these tasks are not threads, nor do they even run on threads. They do not "run" anywhere.

You may find some blog posts of mine useful:

  • async intro, which explains how async/await work.
  • There Is No Thread, which explains how tasks can work without threads.
  • Intro to the Task type, which explains how some tasks (Delegate Tasks) have code and run on threads, but the tasks used by async (Promise Tasks) do not.

Whereas the first creates a new Task on the first await and then everything from there is processed in the new Task?

Not at all. Tasks only complete once, and the method will not continue past the await until that task is complete. So, the task returned by Operation1Async has already completed before Operation2 is even called.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Replacing multiple spaces into just one?

From Dev

Multiple replacement with just one regex

From Dev

Multiple inputs with just one Setter

From Dev

Create one trigger for multiple operations

From Dev

Create one trigger for multiple operations

From Dev

Multiple servlets, or just one main controller

From Dev

mysql/php loop with multiple forms or just one?

From Dev

Multiple publishers in a session, how subscribe to just one?

From Dev

Performing multiple operations in one SSH command

From Dev

MySQL, multiple BIT operations in one column

From Dev

Ubuntu One Sync for multiple folders in Windows, not just the Ubuntu One folder

From Dev

Ubuntu One Sync for multiple folders in Windows, not just the Ubuntu One folder

From Dev

Multiple worker threads vs One worker with async/await

From Dev

Multiple worker threads vs One worker with async/await

From Java

Remove multiple items from a Python list in just one statement

From Java

How can you get a multiple output in json in just one loop?

From Dev

form tag just one value pass instead of multiple

From Dev

Is it better for a method to allow raising multiple types of exceptions or just one?

From Dev

Is it possible to auto-import multiple packages with just one import?

From Dev

Javadoc: Just one comment for multiple global variable declarations

From Dev

INSERT INTO WITH SELECT resulting in multiple rows inserted instead of just one

From Dev

Mongodb, find just one of the names in database when multiple stored

From Dev

Binding multiple ComboBoxes to the same collection but filtering just one

From Dev

How to extract just one of classes of object with multiple classes

From Dev

INSERT INTO WITH SELECT resulting in multiple rows inserted instead of just one

From Dev

form tag just one value pass instead of multiple

From Dev

Mongodb, find just one of the names in database when multiple stored

From Dev

Javadoc: Just one comment for multiple global variable declarations

From Dev

Support multiple screen sizes in Android with just one layout

Related Related

  1. 1

    Replacing multiple spaces into just one?

  2. 2

    Multiple replacement with just one regex

  3. 3

    Multiple inputs with just one Setter

  4. 4

    Create one trigger for multiple operations

  5. 5

    Create one trigger for multiple operations

  6. 6

    Multiple servlets, or just one main controller

  7. 7

    mysql/php loop with multiple forms or just one?

  8. 8

    Multiple publishers in a session, how subscribe to just one?

  9. 9

    Performing multiple operations in one SSH command

  10. 10

    MySQL, multiple BIT operations in one column

  11. 11

    Ubuntu One Sync for multiple folders in Windows, not just the Ubuntu One folder

  12. 12

    Ubuntu One Sync for multiple folders in Windows, not just the Ubuntu One folder

  13. 13

    Multiple worker threads vs One worker with async/await

  14. 14

    Multiple worker threads vs One worker with async/await

  15. 15

    Remove multiple items from a Python list in just one statement

  16. 16

    How can you get a multiple output in json in just one loop?

  17. 17

    form tag just one value pass instead of multiple

  18. 18

    Is it better for a method to allow raising multiple types of exceptions or just one?

  19. 19

    Is it possible to auto-import multiple packages with just one import?

  20. 20

    Javadoc: Just one comment for multiple global variable declarations

  21. 21

    INSERT INTO WITH SELECT resulting in multiple rows inserted instead of just one

  22. 22

    Mongodb, find just one of the names in database when multiple stored

  23. 23

    Binding multiple ComboBoxes to the same collection but filtering just one

  24. 24

    How to extract just one of classes of object with multiple classes

  25. 25

    INSERT INTO WITH SELECT resulting in multiple rows inserted instead of just one

  26. 26

    form tag just one value pass instead of multiple

  27. 27

    Mongodb, find just one of the names in database when multiple stored

  28. 28

    Javadoc: Just one comment for multiple global variable declarations

  29. 29

    Support multiple screen sizes in Android with just one layout

HotTag

Archive