C
C#16mo ago
konakona

❔ ✅ 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
10 Replies
333fred
333fred16mo ago
It all has to do with when you await them In 1, you kick all 3 tasks off, then wait for them to finish. In 2, you wait for each to finish before starting the next one
konakona
konakonaOP16mo ago
ohh thank you wasnt as hard as i though
333fred
333fred16mo ago
Nope 🙂
konakona
konakonaOP16mo ago
appreciate Ok
333fred
333fred16mo ago
yw. Feel free to $close the thread if there's nothing else
MODiX
MODiX16mo ago
Use the /close command to mark a forum thread as answered
konakona
konakonaOP16mo ago
$close
MODiX
MODiX16mo ago
Use the /close command to mark a forum thread as answered
333fred
333fred16mo ago
Literally /close
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server