Avatar of deleyd
deleyd
Flag for United States of America asked on

Difference between 12 ways of waiting?

I can call the following 3 methods, which all achieve the same delay of 2 seconds, in 12 different ways.

I'm interested in the underlying details of these 12 different calls differ under the hood. Exactly what does each do behind the scenes and how do they differ?

        private async Task AsyncDelay()
        {
            await Task.Delay(2000);
        }

        private void FooSleep()
        {
            Thread.Sleep(2000);
        }

        private void TaskDelayWait()
        {
            Task.Delay(2000).Wait();
        }


            FooSleep();
            TaskDelayWait();
            AsyncDelay().Wait();
            Task.Run(() => FooSleep()).Wait();
            Task.Run(() => TaskDelayWait()).Wait();
            Task.Run(() => AsyncDelay()).Wait();
            Task.Run(async () => await AsyncDelay()).Wait();
            await AsyncDelay();
            await Task.Run(() => FooSleep());
            await Task.Run(() => TaskDelayWait());
            await Task.Run(() => AsyncDelay());
            await Task.Run(async () => await AsyncDelay());

Open in new window

C#

Avatar of undefined
Last Comment
Chinmay Patel

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Chinmay Patel

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Your help has saved me hundreds of hours of internet surfing.
fblack61