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?