13
votes

I'm trying to get my head around this code:

 [TestFixture]
 public class ExampleTest
 {
    [Test]
    public void Example()
    {
        AwaitEmptyTask().Wait();
    }

    public async Task AwaitEmptyTask()
    {
        await new Task(() => { });
    }
 }

The method Example never ends and blocks forever. Why??

The fix (from Stubbing Task returning method in async unit test) is to replace await new Task( () => {}) with return Task.FromResult<object>(null); but again, why is this necessary?

I know there are a bunch of questions similar to this one, but none that I've seen seem to explain why this is happening:

3
I suggest you to use Task.Factory.StartNew to not forget to start a taskAlex Zhukovskiy
@AlexJoukovsky I suggest he does not use Task.Factory.StartNew and uses Task.Run instead. There are issues with using StartNew.Scott Chamberlain

3 Answers

21
votes

You're creating a task and never starting it, so it never finishes.

You should be using Task.Run to create a task that you want to start executing immediately, rather than using the Task constructor.

9
votes

You need to call Start() on your task.

Otherwise, it will never finish.

9
votes

Even if you changed it to

await Task.Run(() => { });

it may still never complete. When you hit this line, the program creates a continuation task that will continue when the task completes. The fact that the task is empty is irrelevant. At that point, control flow goes back to

AwaitEmptyTask().Wait();

which waits for the task to complete and blocks the current thread.

When the task does complete, it attempts to continue on the current thread. However, it can't, because the thread is blocked by the call to Wait().

Change the awaited line to

await Task.Run(() => { }).ConfigureAwait(false);

This is a common deadlocking problem. Don't mix Wait() and await. See http://msdn.microsoft.com/en-us/magazine/jj991977.aspx