© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
45 replies
Shinigami

Why does Task 1 prints first instead of Task 3 - Async & Await

internal class Program
    {
        private static async Task Main(string[] args)
        {
            Task firstTask = Task.Run(async ()=> {
                // Thread.Sleep(1000);
                await Task.Delay(10000);
                Console.WriteLine("Task 1");
            });
            // firstTask.Start();
            // Console.WriteLine("YOYOYOY");
            await firstTask;

            Task secondTask = ConsoleWithDelayAsync("Task 2",150);
            Task thirdTask = ConsoleWithDelayAsync("Task 3",9);
            Task fourthTask = ConsoleWithDelayAsync("Task 4",80);

            await secondTask;
            await thirdTask;
            await fourthTask;
        }

        public static void ConsoleWithDelay(string text, int delay){
            Thread.Sleep(delay);
            Console.WriteLine(text);
        }

        public static async Task ConsoleWithDelayAsync(string text, int delay){
            await Task.Delay(delay);
            Console.WriteLine(text);
        }
    }
internal class Program
    {
        private static async Task Main(string[] args)
        {
            Task firstTask = Task.Run(async ()=> {
                // Thread.Sleep(1000);
                await Task.Delay(10000);
                Console.WriteLine("Task 1");
            });
            // firstTask.Start();
            // Console.WriteLine("YOYOYOY");
            await firstTask;

            Task secondTask = ConsoleWithDelayAsync("Task 2",150);
            Task thirdTask = ConsoleWithDelayAsync("Task 3",9);
            Task fourthTask = ConsoleWithDelayAsync("Task 4",80);

            await secondTask;
            await thirdTask;
            await fourthTask;
        }

        public static void ConsoleWithDelay(string text, int delay){
            Thread.Sleep(delay);
            Console.WriteLine(text);
        }

        public static async Task ConsoleWithDelayAsync(string text, int delay){
            await Task.Delay(delay);
            Console.WriteLine(text);
        }
    }


The output I'm getting is

Task 1
Task 3
Task 4
Task 2

Aren't all the Tasks async above? So while Task 1 waits for 10 secs should the rest of the code continue?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ ✅ Baffled by Async-Await and Task
C#CC# / help
4y ago
Why does the following prints "Point" to the console instead of "(2, 3)"?
C#CC# / help
3y ago
✅ async/await Task in an AWS Lambda
C#CC# / help
3y ago
Why use `await AbcAsync()` instead of `Abc()`
C#CC# / help
9mo ago