本文介绍了异步等待和线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于我在MSDN

基本上是说不会创建线程.但是在我继承了HttpClient的类中,我发现了一些有趣的东西.

It is basically saying no thread will be created. But inside a class which I inherited HttpClient 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
        //...
}

该方法在static void Main

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

为什么在await SendAsnc调用后我在另一个线程上?我认为asyn不会创建新线程.或至少在等待后将被调用回原始线程.

Why am I on a separate thread after await SendAsnc call?? I thought asyn creats no new thread. Or at least it will be invoked back to the original thread after await.

推荐答案

在文档中很少对此进行宣传,但是由于以下原因,异步/等待在控制台应用程序中的工作方式与在UI应用程序中的工作方式大不相同:控制台应用程序中缺少同步上下文. 本文描述了详细信息并提供了一个代码示例,说明如何添加一个代码,以便异步/等待以更可预测的方式表现.

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

通常,对于异步/等待(不必不必要)需要多线程(尽管诸如Task.Run do 之类的事情实际上会导致所涉及的代码在线程池中运行),但是(如我所链接的文章中所述)在控制台应用程序async/await中可以在任何地方运行.

Normally, you're absolutely correct about async/await not necessarily entailing multithreading (although things like Task.Run do, in fact, cause the code in question to run in the thread pool), but (as described in the article I linked to) in a console application async/await could run anywhere.

关于在同一线程上运行时异步如何工作的我通常的比喻是考虑与另外9个人(所以总共10个人)一起去一家餐馆.当服务员到来时,有9个人已经准备就绪,而第10个人还没有准备好.在这种情况下,服务员将首先接受其他9个人的订单,然后再返回第10个人.如果出于任何原因,第十个人真的很慢,服务员总是可以将订单带回厨房,直到第十个人准备好了.

My usual analogy for how async works when running on the same thread is to think of going to a restaurant with 9 other people (so 10 people total). When the waiter comes by, 9 of the people are ready and the 10th isn't. In this case, the waiter will take the other 9 people's orders first and then come back to the 10th person. If for whatever reason the 10th person's being really slow the waiter could always bring the orders back to the kitchen and come back when the 10th person's ready.

很显然,引进第二名服务员只是为了等待第十个人准备好点,毫无意义的是,让其他所有人都等待一个家伙准备好是没有效率的.在这种情况下增加额外的服务员并不会加快事情的进展,因为延误不是由于缺少服务员引起的,这是由第十个人下定决心的原因造成的(服务员对此无能为力) ).

Clearly, there's no point to bringing in a second waiter just to wait for the 10th guy to be ready to order, and it would clearly be inefficient to make everyone else wait for the one guy to be ready. Adding extra waiters to the situation won't speed things up because the delay isn't being caused by a lack of waiters, it's caused by the 10th guy being slow to make up his mind (and there's nothing the wait staff can do about that).

这篇关于异步等待和线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 20:58