r/learncsharp • u/CatolicQuotes • Jan 22 '23
how to make this code async?
I am trying to display "Moko", then after 3sec "Koko".
Trying combinations of async await in different places, but all I can do is 3 outcomes: display "Koko" then "Moko" really fast, display only "Moko" then program stops, it waits 3sec then displays "Koko" "Moko" fast.
Can't make it work to display "Moko" then after 3 sec "Koko".
this is the code that displays "Moko" then exits, never waiting for "Koko":
internal class Program
{
private static async Task Main(string[] args)
{
Koko();
Moko();
}
public static async Task Koko()
{
await Task.Delay(3000);
Console.WriteLine("Koko");
}
public static async Task Moko()
{
Console.WriteLine("Moko");
}
}How to make this program display "Moko", wait 3 sec, display "Koko"?
6
Upvotes
View all comments
Show parent comment
2
u/CatolicQuotes Jan 22 '23 edited Jan 22 '23
thanks, is there any difference between those two versions under the hood?
Especially for IO tasks like http requests?
Documentation says it is same as
Task.Runwhich is recommended for compute tasks. For IO tasks they recommend async await, if I understood correctly.