1

In my attempt to understand async-await, I've written a simple program

private static async Task<int> GetIntAsync()
{
    Task<int> getInt = GetInt();
    DoSomeMoreIndependentWork();
    int i = await getInt;
    SomeStuffAfterTheAwait();
    return i;
}

public static void Main()
{
    Task.Run(async () => 
    {
        Task<int> getIntAsync = GetIntAsync();
        DoIndependentWork();
        int result = await getIntAsync;
        Console.WriteLine(result);          
    }).Wait();
}

and want to understand it in terms of the graph from here.

Here's what I think is a possible flow of execution if DoIndependentWork and DoSomeMoreIndependentWork take a long time to execute:

|<--Launch GetInt-->||<-- DoSomeMoreIndependentWork-->||<-- DoIndependentWork -->||<-- Wait for GetInt to finish -->||<-- SomeStuffAfterTheAwait -->||<-- Console.WriteLine(result) -->|

                     |<------ Do GetInt .................................................................... ------>|

Is that correct? If not, what is a better representation of some or all of the possible flows of execution?

Community
  • 1
  • 1
user7127000
  • 3,143
  • 6
  • 24
  • 41
  • Where is the code for `GetInt`? `await` doesn't make anything run asynchronously, it awaits already asynchronous operations. Unless there is some real asynchronous code in there, that's just a blocking method. In which case, *all* methods execute sequentially – Panagiotis Kanavos Dec 29 '16 at 10:05

0 Answers0