Why does this code ignore the await and proceed anyway?

Josh

I have the following code:

public Index () {
    InitializeIndexAsync();
}

async Task InitializeIndexAsync () {
    State = IndexState.Initializing;
    await Task.Factory.StartNew(async () => {
        // Initialize other things.

        await IndexAsync();
    });

    State = IndexState.Ready;
}

I would expect that "State = IndexState.Ready" would not be hit until the asynchronous lambda completes, but debugging shows that line is hit long before the thread started above it completes. Why is this?

Stephen Cleary

StartNew does not understand async lambdas, so when you pass it an async lambda, it will return a Task<Task>. Conceptually, the "outer" task only represents the start of the async lambda; the "inner" task represents the completion of the async lambda.

This is one of the reasons that StartNew is the wrong choice for async code, as I explain on my blog. A better solution is to use Task.Run, which was designed with async in mind:

async Task InitializeIndexAsync () {
  State = IndexState.Initializing;
  await Task.Run(async () => {
    // Initialize other things.

    await IndexAsync();
  });

  State = IndexState.Ready;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

IntelliJ IDEA: Exception removal will break source code. Proceed anyway?

From Dev

Warning: implicit declaration of function — why does my code work anyway?

From Dev

Why does my TCS not await?

From Dev

Why does Git ignore these folders?

From Dev

Why does FloatToStr ignore ThousandSeparator?

From Dev

Why does setInterval() ignore errors?

From Dev

Why does GLSL ignore return?

From Dev

Why does sudo ignore aliases?

From Dev

Why does bash ignore SIGTERM?

From Dev

Why Gulp does not ignore the file?

From Java

Why does mutating a list in a tuple raise an exception but mutate it anyway?

From Dev

I don't fully understand D2D1_FIGURE_BEGIN: why is it needed, what's the difference, and why does Microsoft's sample code mismatch types anyway?

From Dev

Why does the Visual Studio debugger not show the state machine code generated by the await keyword?

From Dev

Why does Typescript ignore my tsconfig.json inside Visual Studio Code?

From Dev

Why does my synchronized code block seem to ignore another, waiting synchronized block on the same object?

From Dev

Why does my return statement ignore the rest of my code in a function in python?

From Dev

No "Proceed Anyway" option on ERR_CERT_COMMON_NAME_INVALID in Chrome

From Dev

How to proceed with Task, await and async in background

From Dev

Why doesn't the program proceed

From Dev

EXECUTE fail does not proceed next

From Dev

Where does the code following an await run

From Dev

Why does async await throw a NullReferenceException?

From Dev

Why await of Condition releases the lock but signal does not?

From Dev

Why does thenComposeAsync await the return to be redeemable

From Dev

Why does AngularJS ignore $scope field change?

From Dev

Why does make ignore my modifications?

From Dev

Why does Python requests ignore the verify parameter?

From Dev

Why does Git ignore the .vs directory?

From Dev

Why does SQL Anywhere ignore the @ named parameter?

Related Related

  1. 1

    IntelliJ IDEA: Exception removal will break source code. Proceed anyway?

  2. 2

    Warning: implicit declaration of function — why does my code work anyway?

  3. 3

    Why does my TCS not await?

  4. 4

    Why does Git ignore these folders?

  5. 5

    Why does FloatToStr ignore ThousandSeparator?

  6. 6

    Why does setInterval() ignore errors?

  7. 7

    Why does GLSL ignore return?

  8. 8

    Why does sudo ignore aliases?

  9. 9

    Why does bash ignore SIGTERM?

  10. 10

    Why Gulp does not ignore the file?

  11. 11

    Why does mutating a list in a tuple raise an exception but mutate it anyway?

  12. 12

    I don't fully understand D2D1_FIGURE_BEGIN: why is it needed, what's the difference, and why does Microsoft's sample code mismatch types anyway?

  13. 13

    Why does the Visual Studio debugger not show the state machine code generated by the await keyword?

  14. 14

    Why does Typescript ignore my tsconfig.json inside Visual Studio Code?

  15. 15

    Why does my synchronized code block seem to ignore another, waiting synchronized block on the same object?

  16. 16

    Why does my return statement ignore the rest of my code in a function in python?

  17. 17

    No "Proceed Anyway" option on ERR_CERT_COMMON_NAME_INVALID in Chrome

  18. 18

    How to proceed with Task, await and async in background

  19. 19

    Why doesn't the program proceed

  20. 20

    EXECUTE fail does not proceed next

  21. 21

    Where does the code following an await run

  22. 22

    Why does async await throw a NullReferenceException?

  23. 23

    Why await of Condition releases the lock but signal does not?

  24. 24

    Why does thenComposeAsync await the return to be redeemable

  25. 25

    Why does AngularJS ignore $scope field change?

  26. 26

    Why does make ignore my modifications?

  27. 27

    Why does Python requests ignore the verify parameter?

  28. 28

    Why does Git ignore the .vs directory?

  29. 29

    Why does SQL Anywhere ignore the @ named parameter?

HotTag

Archive