Link to home
Start Free TrialLog in
Avatar of deleyd
deleydFlag 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

ASKER CERTIFIED SOLUTION
Avatar of Chinmay Patel
Chinmay Patel
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial