Do I have to await an async method?

前端 未结 2 1841
生来不讨喜
生来不讨喜 2021-01-22 04:12

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

2条回答
  •  [愿得一人]
    2021-01-22 04:57

    No, nothing requires you to await the call. The call is just returning a Task 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."

提交回复
热议问题