Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

Can a single-threaded program be asynchronous?

Ayyaz Sheikh

Yes, a single-thread program can be asynchronous. Making a method/ function async does not mean creating threads in the background.

class HelloWorld
{
    static void Main(string[] args)
        {
            Method1();
            Console.WriteLine("Main method thread");
            Console.Read();

        }

        static async void Method1()
        {
            await Task.Delay(10000);
            Console.WriteLine("Async Method finishes");
            
        }
}

If you execute the above code snippet, look up the running threads at lines 13 and 14. You will notice that a Worker Thread will be created, not a new thread but a resuming thread. It tells the compiler to resume the execution from the line where await was called.

Note: Asynchronous programs are not always multi-threaded.

RELATED TAGS

CONTRIBUTOR

Ayyaz Sheikh
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring