How is resuming from waiting?

I read Eric Lippert blog posts on Asynchrony in C # 5 ( part 4 that were especially important) and watched the Anders PDC10 announcement by subject, and I don't understand how extensions from asynchronous methods are resumed in a single streaming context.

Both sources discuss the use of asynchronous methods in the same user interface flow path to improve responsiveness, and in Anders' example he mentions that when the asynchronous task completes, its continuation is planned by adding a message to the message pump.

Does the asynchronous method really know that it should do what seems context-specific, or is it a simplification?

More generally, how can you resume asynchronous methods that are processed in the same threading context? Is there a need for planning within a single thread?

+6
c # async-await
source share
2 answers

A continuation of the task knows where the continuation should be planned - for example. "any thread thread thread" or "user interface thread".

This behavior is defined by awaiter, however, it is actually not part of what the C # compiler is responsible for. The compiler simply calls BeginAwait and proceeds to continue; awaiter returns a boolean value indicating whether the task was completed synchronously, or whether the caller should return and continue asynchronously.

So, at the moment this decision is made in awaiter returned by TaskEx , but I will not be surprised to see that all this will end up in Task . This can convey things such as a synchronization context that knows how to handle further actions.

I'm not quite sure what really single-threaded context you are considering ... or are you thinking about a situation where the main part of the work should happen in one thread, but other threads may be (for example, when an HTTP packet is received, the input completion port is processed in the thread output and response processed back in the user interface thread)?

+7
source share

John's answer, of course, is wonderful; I thought I would just add one more thing.

View the WinForms application with a single button in a form that runs code when the button is clicked.

What happens if you do not press a button? Nothing. The process exists, the code works, but it seems to be doing nothing. In fact, what he does is to process the messages in the user interface thread and determine that none of them are interesting, but it does not look like he is doing something interesting.

When you click a button, unexpectedly one of these messages is interesting, and the message pump knows that when it sees this click event, it must run some code. The way it is.

The asynchronous scenario in one thread is one and the same. The continuation - “what to do after the task is completed” - is actually the “event completed” event handler of the event. When the task completes, it “clicks the button” and completes the message in the message queue of the user interface thread. It doesn’t matter if it does this from the user interface thread or from the I / O completion thread or something else. When the user interface thread approaches processing this message, it causes a continuation. In the same way as when a user interface thread is processed by a click of a button, it invokes a click handler.

+7
source share

All Articles