I write the sample code based on the tutorial.
I think when TestAsync Task runs, the "Logic A" will be printed and then with the "await" keyword "Logic C" be printed.
But it actually runs just like a synchronized way, print "Logic C" then "Logic A"
class Program
{
static void Main(string[] args)
{
TestAll();
}
static async void TestAll()
{
var y = TestAsync();
// I want to do other jobs before task y finish
Console.WriteLine("Logic A");
int value = await y;
Console.ReadLine();
}
static Task<int> TestAsync()
{
for (int i = 0; i < 20000; i++)
;
Console.WriteLine("Logic C");
return Task.FromResult(0);
}
}
But why print Logic C then Logic A ?
Thanks the answers, then another question: I expect "Logic B" --> "Logic A" --> "Logic C" --> "1"
But it actually only prints "Logic B" --> "Logic A" and finished.
class Program
{
static void Main(string[] args)
{
TestAll();
}
static async void TestAll()
{
var y = TestAsync();
// I want to do other jobs before task y finish
Console.WriteLine("Logic A");
// delete this line will be correct, but I don't know why
int value = await y;
Console.WriteLine(value);
Console.ReadLine();
}
static async Task<int> TestAsync()
{
Console.WriteLine("Logic B");
await Task.Delay(100);
Console.WriteLine("Logic C");
return 1;
}
}
