我必须等待异步方法吗? [英] Do I have to await an async method?
问题描述
我使用仅支持异步方法的HttpClient
.
I use a HttpClient
that only supports async methods. Do I have to await
them in the calling method (not in the async method itself, I have to await the calls in HttpClient
methods as far as I know)?
在那个项目中我真的不需要多线程.我会很好地阻止并等待直到收到HttpClient
的响应,因为无论如何我都需要数据.这是一个Console
应用程序.
I don't really need multithreading in that project. I would be fine blocking and waiting until I get a response from HttpClient
as I need to have the data anyway. This is a Console
application.
推荐答案
否,不需要任何操作来await
通话.该呼叫只是将Task<T>
返回给您,而不是直接返回T
.如果在Task
上调用Result
属性:
No, nothing requires you to await
the call. The call is just returning a Task<T>
to you, rather than a T
directly. If you call the Result
property on the Task
:
var result = client.MakeCallAsync().Result;
您正在告诉它我不在乎它是否阻塞,我希望我的代码(和该线程)在这里等待直到完成."
You are telling it "I don't care if it blocks, I want my code (and this thread) to wait here till this is done."
这篇关于我必须等待异步方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!