konakona
konakona
CC#
Created by konakona on 8/25/2023 in #help
❔ ✅ Why does it work like that
I'm learning async, came up with this code. Found some info but my dumb ass didn't understand it.
// 1
var first = AsyncPrint(1);
var second = AsyncPrint(2);
var third = AsyncPrint(3);

await first;
await second;
await third;

// 2
await AsyncPrint(1);
await AsyncPrint(2);
await AsyncPrint(3);
// 1
var first = AsyncPrint(1);
var second = AsyncPrint(2);
var third = AsyncPrint(3);

await first;
await second;
await third;

// 2
await AsyncPrint(1);
await AsyncPrint(2);
await AsyncPrint(3);
public static async Task AsyncPrint(int index)
{
Console.WriteLine($"print {index} started");
await Task.Delay(5000);
Console.WriteLine($"print {index} stopped");
}
public static async Task AsyncPrint(int index)
{
Console.WriteLine($"print {index} started");
await Task.Delay(5000);
Console.WriteLine($"print {index} stopped");
}
why does 1 work as async threads (~5 seconds) and 2 works like regular ones (~15 seconds)? Either way I use a Task with await
13 replies