all the async
keyword does is allow you to use the await
keyword inside a function. If you don't call await
the function behaves the same as if you did not do any async
keyword at all.
Take a look at this image from the msdn page "Asynchronous Programming with Async and Await (C# and Visual Basic)"
If you follow the black line all that happens on the same thread, unbroken until you get to 6
. Then at that point the function returns and continues later when the results are ready. Because you don't have a await
in your code your "black line" goes through your entire function.
To make it work like you want to you need to signal in the function where it should go back to the caller.
async Task<int> Method()
{
for (int i = 0; i < int.MaxValue ; i++)
{
await Task.Yield(); //Your function now returns to the caller at this point then comes back later to finish it. (Your program will be much slower now because it is going to wait int.MaxValue times.)
}
return 10;
}