I have the following code
class Program
{
public async Task<bool> StartMyTask()
{
await Foo();
return true;
}
public async Task<bool> Foo()
{
for (int i = 0; i < 1000000; i++)
{
Console.WriteLine("Loop");
}
return true;
}
static void Main(string[] args)
{
Program obj = new Program();
var myTask = obj.StartMyTask();
Console.WriteLine("Before Task Return");
Console.ReadLine();
}
}
According to my understanding when "await Foo()" gets called, a thread will be created which will execute the "Foo()" method and the control would be returned back to the caller (Main method).
By considering this, "Before Task Return" should be printed before the "Foo()" method completes. But its not happening, first the "Foo()" method completes and then "Before Task Return" is displayed.