The asynchronous method does not require a separate thread; it is executed in the current synchronization context. +

What does it mean: - "uses time in the stream, only when the method is active"?

Does the async method return control to an external code and only execute on the same thread at the same time when active?

What is the meaning of this phrase?

enter image description here

Thank you in advance!

Resource: https://docs.microsoft.com/ru-ru/dotnet/csharp/programming-guide/concepts/async/index

    1 answer 1

    When the async method is called, the following occurs: The async method starts to run synchronously. If this method ends before the first await , the result is delivered synchronously, and the already completed, completed Task returned from the method. If await encountered during execution, the system checks to see if the task that await was called on has completed. If this task has completed, then its result is substituted, and synchronous execution continues further. If the task on which await occurs has not yet been completed, at that moment an incomplete Task is returned from the method. The method is marked as executed and control is returned to the external code. At this point, the outer code takes control and continues to execute. And since this Task is not completed yet, the external code at this moment will similarly give control to an even more external code. When the Task to which await occurs ends (producing a result or an exception), the code after await resumes its work. This all means that when the async method is executed synchronously, it uses the time of the main thread while it is being executed, and does not take the time of the main flow.

    Thus, it turns out that if the main thread in which the async method is running is not active, the async method does not work either. From here this expression - "uses time in the stream, only when the method is active."

    • նդձ թվում է, ես սա արդեն այստեղ էի տեսել :) - VladD
    • @VladD Does this mean that you are armenian or just an example of using google translator? And yes
    • I am not Armenian, but the world is small, and there will be Armenian friends with me. But still, if you quote, put a link to the original. Peace! :) - VladD
    • Okay already added;) - Vardan Vardanyan