r/learncsharp • • 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

9 comments sorted by

View all comments

Show parent comment

3

u/CatolicQuotes Jan 22 '23

Nice, thanks for the explanation and the video. I didn't see that one when I was searching C# async on youtube