Asynchronous wait and threads

based on what i read on msdn

Asynchronous and pending keywords do not cause additional threads to be created. Asynchronous methods do not require multithreading, since the asynchronous method does not start in its thread. The method works on the current synchronization and uses the time in the stream only when the method is active. You can use Task.Run to move the work associated with the processor to the background thread, but the background thread does not help the process, which just waits for the results to become available.

Basically they say that the thread will not be created. But inside the class that HttpClient inherited, I found something interesting.

 async Task<T> SendAsync<T>(HttpMethod method, Uri uri, HttpContent content, CancellationToken cancellationToken) { HttpRequestMessage msg = new HttpRequestMessage(method, uri); msg.Content = content; //On Main Thread HttpResponseMessage response = await SendAsync(msg, cancellationToken); //On worker thread //... } 

method is called inside static void Main

 Task result = client.GetAsync<string>(...); //GetAsync calls await SendAsync<T> result.Wait(); 

Why am I in a separate thread after calling await SendAsnc ? I thought asin was not creating a new thread. Or at least it will be called back to the original stream after waiting.

+5
source share
3 answers

This is poorly advertised in the documentation, but the way async / await works in a console application is very different from how it works in a user interface application due to the lack of synchronization context in the console application. This article describes the details and gives sample code on how to add one so that the asynchronous / waiting behaves in a more predictable way.

As a rule, you are absolutely right in async / await, which does not necessarily entail multithreading (although things like Task.Run actually lead to the fact that the code in question runs in the thread pool), but (as described in the article I'm connected with ) in async / await console application can work anywhere.

My usual analogy to how async works when working on the same thread is to think of going to a restaurant with 9 other people (10 in total). When the waiter arrives, 9 people are ready, but 10 are not. In this case, the waiter will first take the remaining 9 people, and then return to the 10th person. If for some reason the 10th person is really slow, the waiter can always return orders to the kitchen and return when the 10th person is ready.

It is clear that it makes no sense to attract a second waiter to wait until the 10th guy is ready to order, and it would be clearly ineffective to make everyone else wait until one guy is ready. Adding additional waiters to the situation will not accelerate the situation, because the delay is not caused by the shortage of waiters, due to the fact that the 10-year-old guy slowly decides (and there is nothing that can wait until the waiting staff can do this).

+9
source

I describe this behavior in my async blog post .

In short, this is correct:

Asynchronous and pending keywords do not create additional threads.

And like this:

I thought Asyn has no new thread.

This is not true:

Or at least it will be called back to the original stream after waiting.

async and await - by themselves - do not create any additional threads. However, after await completes, the rest of the async method should execute somewhere.

By default, await captures the "context" that is the current SynchronizationContext (if it is not null , in which case the current context is the current TaskScheduler ). User interface threads have their own synchronization context (for example, WinFormsSynchronizationContext or DispatcherSynchronizationContext ), in which case the async method will continue to run in the same user interface thread.

There is no synchronization context in the console application, so the "context" captured by await will be the context of the thread pool. That way, when the async method is ready to resume, it will be assigned to a thread pool and picked up by some thread.

+6
source

Console applications have a SynchronizationContext thread pool, rather than a synchronous SynchronizationContext, which is used by GUI and ASP.Net applications, so when the wait completes, it schedules the rest of the async method to the thread thread stream. read this article for more information on asynchronous wait.

+1
source

Source: https://habr.com/ru/post/1258772/


All Articles