Await callback order RRS feed

  • Question

  • It seems that behind the scenes, the Await keyword schedules a continuation of the code using the current ExecutionContext. Does the scheduler run registered callbacks/continuations in LIFO or FIFO (last-in-first-out/first-in-first-out) order?

    In other words, when the same task is Awaited at two point in the code, are the callbacks guaranteed to occur in the same order that the Await keyword was reached, are they in backwards order, or is there no guarantee at all?

    Thursday, August 7, 2014 7:27 PM

All replies

  • Hi Saksware,

    when the task is awaited on line 2 and on line 3, first the await on line 2 is awaited. The code on line 3 didn't run yet. And when the task comes back, the code on line 3 is awaited.

    So it's exactly in the order as you write it.

    Thomas Claudius Huber

    "If you can't make your app run faster, make it at least look & feel extremly fast"

    My latest Pluralsight-course: Windows Store Apps - Data Binding in Depth

    twitter: @thomasclaudiush
    author of: ultimate Windows Store Apps handbook | ultimate WPF handbook | ultimate Silverlight handbook

    Thursday, August 7, 2014 9:25 PM
  • Thanks for your response. It sounds like you understood the question to mean that there are two consecutive Awaits in the same method. Of course in that case you're correct, that the second Await isn't even executed until the first Await returns. I'm talking about to Awaits waiting the same task in two different methods - the first Await returns execution to the caller, and then a user event triggers a different method that performs a synchronous Task.Wait on the task, but must also be guaranteed that the first Await gets continued before continuing. If the Await scheduling is FIFO, then I could Await in the second method, assured that the first Await will be continued before control is passed back to the second method.
    Friday, August 8, 2014 12:23 PM