Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my TCS not await?

The async keyword do cause the CIL to change (even if there's no await inside the method), but it is primarily to allow await to be present.

But I did not expect the following to happen:

static void Main(string[] args)
{
    Task t = Go();
    t.Wait();
}

static async Task Go()
{
    Console.WriteLine(1);
    await AAA(3000);
    Console.WriteLine(2);
}


static  Task<object> AAA(int a) // <--- No `async`
{
    TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
    Task.Delay(a).ContinueWith(b => tcs.SetResult(null));
    return tcs.Task;
}

This print:

1
(wait)
2

But if I change

static  Task<object> AAA(int a) 

to

static async  Task<object> AAA(int a) 

It prints:

1
2
(no wait)

Question

Why don't I see the delay? The TCS is only resolved after three seconds. Meanwhile, the task is not resolved and should be awaited.

like image 968
Royi Namir Avatar asked Sep 25 '15 15:09

Royi Namir


3 Answers

Without the async keyword you are returning the TaskCompletionSource's task from AAA and so you wait for it to complete (which will happen after the delay completes).

However, when you add the async keyword , the task returned from the method is the state-machine's task that completes synchronously. That task has inside it (as a result) the TaskCompletionSource's task but that isn't the task you are waiting on.

If you want that method to wait for the TaskCompletionSource's task you can await the inner task of Task<Task>:

await ((Task) await AAA(3000));

Or await the TaskCompletionSource's instead of returning it:

async Task AAA(int a)
{
    TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
    Task.Delay(a).ContinueWith(b => tcs.SetResult(null));
    await tcs.Task;
}

Or even better, just awaiting the Task.Delay itself:

async Task<object> AAA(int a)
{
    await Task.Delay(a);
    return null;
}
like image 155
i3arnon Avatar answered Nov 14 '22 08:11

i3arnon


Because when you return Task from an async method with a return type of Task<object> what you get it Task<Task> with your task (that you expect to be awaited) inside. That what you should do:

static async Task<object> AAA(int a)
{
  await Task.Delay(a);
  return null;
}

In short, try to avoid mixing async-await and direct task operations in one method.

like image 44
Serg Rogovtsev Avatar answered Nov 14 '22 09:11

Serg Rogovtsev


As Serg mentioned, look at this example in Visual Studio:

        static async Task<int> AAA(int a) // <--- No `async`
        {
             TaskCompletionSource<int> tcs = new TaskCompletionSource<int>();
             Task.Delay(a).ContinueWith(b =>
             {
                  tcs.SetResult(1);
             });
             return tcs.Task;
        }

You will get error like this:

Since this is an asynchronous method, the return expression must be of type 'int' rather than 'Task', because your method will run synchronously. That's why it is required to return int, not Task.

like image 2
razor118 Avatar answered Nov 14 '22 09:11

razor118