0

var task = Task.Run(() => DoSomeStuff()).Result;

What happens here under the hood?

I made a tiny test:

using System;
using System.Threading.Tasks;

public class Program
{
    public static void Main()
    {
        var r = Task.Run( () => {Thread.Sleep(5000); return 123; }).Result;
        Console.WriteLine(r);
    }
}

It prints "123" after 5s. So does accessing any such property on Task act as a shortcut to calling Task.Wait() i.e. is this safe to do?


Previously my code called Task.Delay(5000) which returned "123" immediately. I fixed this in my question but leave this here as comments and answers reference it.

4
  • 1
    It blocks as if .Wait() was called. The test never awaits for Task.Delay(). No, accessing any property isn't a shortcut to Wait(). The behaviour of Result and Wait() are well defined - Wait() blocks when we don't want any results. Result blocks and returns the result Feb 5, 2020 at 12:34
  • 1
    The important point that has not been made in an answer yet is: Never use either Wait or Result unless you know that the task has already completed. It is very easy to get into a situation where the task has scheduled work onto the thread that you just put asleep, and now the task will never complete and the thread will never wake up because the worker that will work on the task is sleeping until it is finished! Asynchrony is designed to be asynchronous; forcing it to be synchronous is dangerous. Feb 5, 2020 at 16:07
  • 1
    @EricLippert only wait for things that have already happened? This seems rather unintuitive!
    – Mr. Boy
    Feb 5, 2020 at 16:13
  • 1
    That comment really should be an answer. Will add one. Feb 5, 2020 at 16:13

3 Answers 3

6

You asked two questions. First, does accessing Result implicitly cause a synchronous Wait? Yes. But the more important question is:

is this safe to do?

It is not safe to do this.

It is very easy to get into a situation where the task that you are synchronously waiting on has scheduled work to run in the future before it completes onto the thread that you just put to sleep. Now we have a situation where the thread will not wake up until the sleeping thread does some work, which it never does because it is asleep.

If you already know that the task is complete then it is safe to synchronously wait for the result. If you do not, then it is not safe to synchronously wait.

Now, you might say, suppose I know through some other means that it is safe to wait synchronously for an incomplete task. Then is it safe to wait? Well, by the assumption of the question, yes, but it still might not be smart to wait. Remember, the whole point of asynchrony is to manage resources efficiently in a world of high latency. If you are synchronously waiting for an asynchronous task to complete then you are forcing a worker to sleep until another worker is done; the sleeping worker could be doing work! The whole point of asynchrony is to avoid situations where workers go idle, so don't force them to.

An await is an asynchronous wait. It is a wait that means "wait on running the rest of the current workflow until after this task is done, but find something to do while you are waiting". We did a lot of work to add it to the language, so use it!

12
  • I'm still trying to get my head around this, is a trivial example possible of how I can easily get into the situation you describe?
    – Mr. Boy
    Feb 5, 2020 at 16:30
  • 1
    @Mr.Boy: Absolutely there is. Stephen gives some examples here: blog.stephencleary.com/2012/07/dont-block-on-async-code.html In general if you want cogent explanations for bad patterns in async, Stephen should be your go-to. Feb 5, 2020 at 16:31
  • 1
    @Mr.Boy the async/await is a complex technology with a simple interface. Hard work has been invested to make it behave correctly with the least effort possible. Just avoid all the fancy stuff. Avoid async void, avoid Wait and Result, avoid ContinueWith, avoid Task.Run, avoid fire and forget tasks, forget about TaskSchedulers, and just focus on using plain vanilla async/await everywhere that the platform provides you with a built-in async method. You'll notice that the right thing will happen automagically. Feb 5, 2020 at 17:04
  • 1
    @Mr.Boy: You are 100% correct that you should not use this sharp tool until you have a good understanding of it. You are 100% wrong that trying to force an asynchronous workflow to become synchronous is the right thing to do in that situation. Learn how asynchrony works and then embrace it; don't take a hammer to it to force it to work like synchronous code. Feb 5, 2020 at 18:33
  • 2
    @Mr.Boy if you are comfortable with old school continuations (ContinueWith) and uncomfortable with async/await magic, then by all means you can keep using the old stuff. They are still working as well as ever. Just avoid mixing them with the new stuff. Combining classic TPL techniques with async/await is a recipe for frustration and problems. Feb 5, 2020 at 20:51
3

So does accessing any such property on Task act as a shortcut to calling Task.Wait()?

Yes.

From the docs:

Accessing the [Result] property's get accessor blocks the calling thread until the asynchronous operation is complete; it is equivalent to calling the Wait method.

However, your test doesn't do what you think it does.

Task.Delay(..) returns a Task which completes after the specified amount of time. It doesn't block the calling thread.

So () => { Task.Delay(5000); return 123; } simply creates a new Task (which will complete in 5 seconds), then throws it away and immediately returns 123.

You can either:

  1. Block the calling thread, by doing Task.Delay(5000).Wait() (which does the same thing as Thread.Sleep(5000))
  2. Asynchronously wait for the Task returned from Task.Delay to complete: Task.Run(async () => { await Task.Delay(5000); return 123; })
2
  • A neat feature to run and wait on a Task as a 1-liner then. Thanks for the good answer
    – Mr. Boy
    Feb 5, 2020 at 15:35
  • True, it's mainly for setting up a Task oriented code-base and testing with the simple cases. I was trying to think if it makes any difference?
    – Mr. Boy
    Feb 5, 2020 at 15:44
2

The test doesn't wait for Task.Delay() so it returns immediatelly. It should be :

var r = Task.Run(async  () => { await Task.Delay(5000); return 123; }).Result;

The behavior of Result is well defined - if the task hasn't completed, it blocks until it does. Accessing other Task properties doesn't block

9
  • Yeah I was just trying to make a Task that simulated something taking a few seconds.
    – Mr. Boy
    Feb 5, 2020 at 15:16
  • 2
    @Mr.Boy: Never call Thread.Sleep with a non-zero argument for anything except test cases that simulate work, like you are doing here. Certainly never use it in production code to institute a delay. If you had a workflow in a real office with real workers, you would never say "This mail should go out on the first of next month. Mary, fall asleep and set an alarm for next month and then send the mail; we'll keep paying your salary and benefits while you sleep". Workers are expensive resources and the whole point of asynchrony is to not put them to sleep! Feb 5, 2020 at 16:17
  • 1
    @EricLippert: I've seen a few people (e.g., Joe Duffy) recommend Thread.Sleep(1) over Thread.Sleep(0) for this purpose. The difference is that Thread.Sleep(1) unconditionally removes the thread from the scheduler, allowing time slice for other threads, even if those threads have lower priority.
    – Brian
    Feb 5, 2020 at 19:47
  • 2
    @Brian: And yes, "always take Joe's advice" is one of my base rules for multithreaded programming, right after "don't do it at all if you can avoid it". :) Feb 5, 2020 at 19:58
  • 2
    Nevermind. Everything I/Joe said is outdated.
    – Brian
    Feb 5, 2020 at 20:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.