We help IT Professionals succeed at work.
Get Started

Difference between 12 ways of waiting?

deleyd
deleyd asked
on
80 Views
Last Modified: 2020-02-25
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

Comment
Watch Question
Chief Technology Ninja
CERTIFIED EXPERT
Distinguished Expert 2020
Commented:
This problem has been solved!
Unlock 1 Answer and 1 Comment.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant

An Experts Exchange subscription includes unlimited access to online courses.

Get Started
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE