简体   繁体   中英

Do I have to await an async method?

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)?

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.

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."

I don't really need multithreading in that project.

async is about asynchrony, not multithreading.

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.

In this case, then, I'd just say to keep everything synchronous. That is, use WebClient instead of HttpClient .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM