1

Possible Duplicate: Is Async await keyword equivalent to a ContinueWith lambda?

Edit:

I see this question has been marked as a duplicate, but it's not quite the same. I'm specifically asking about whether await is the equivalent of ContinueWith IN CONJUNCTION WITH TaskContinuationOptions.AttachedToParent

Is this (inside an async method body)

    await SomeMethodAsync();

    Console.WriteLine("hi");

The equivalent to

    Task.ContinueWith(delegate() {

        Console.WriteLine("hi");

    }, TaskContinuationOptions.AttachedToParent);

}

?

The only answer I could find on Google says that these are not equivalent, but I don't believe it because -

In the first example the async method body returns the Task to the user as soon as it hits the first await, and if the task was not waiting on the child task (because of AttachedToParent option) then it would be completed already.

My sanity depends on this being the case.

The answer I found on Google is here http://social.msdn.microsoft.com/Forums/en-US/async/thread/bec2151a-abfd-43b9-a2e0-ffe34ae481f6/

Community
  • 1
  • 1
NoPyGod
  • 4,905
  • 3
  • 44
  • 72
  • What exactly is your goal ? IMHO this is an implementation-detail you should NOT depend on... – Yahia Jan 11 '13 at 21:27
  • 1
    My goal is to understand what is happening under the hood so I have a better knowledge of the entire framework. – NoPyGod Jan 11 '13 at 21:29
  • 1
    You should really believe what Stephen Toub says about C#'s await model. He's a very reliable source on the subject. – Servy Jan 11 '13 at 21:32
  • @NoPyGod then take Reflector or similar and look at the IL... it will differ with compiler version/settings etc. but it will give you real insight on how it works... BTW: I second that Stephen Toub is right in his answer and indeed a very reliable source... – Yahia Jan 11 '13 at 21:47

1 Answers1

0

So Stephen Toub is obviously quite an authoritive figure...

I did some testing and I see now why await does not need to create its continuation with AttachedToParent.

It's because this

await SomeMethodAsync();

is receiving the result of the continuation, and then the parent task continues where it left off

NoPyGod
  • 4,905
  • 3
  • 44
  • 72