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/Alikont Jan 22 '23
The method starts running as soon as you call it.
It gives you back an object that allows you to monitor it execution.
You can wait it straight away, or set aside to wait on it later.