Deadclock on calling async methond

时间:2021-01-04 05:56:13

Issue:

HttpClient.GetAsync(…) never returns when using await/async

Related Posts:

http://*.com/questions/9895048/async-call-with-await-in-httpclient-never-returns

http://*.com/questions/10343632/httpclient-getasync-never-returns-when-using-await-async/10351400#10351400

Solution:

Quick fix from here. Instead of writing:

Task tsk = AsyncOperation();
tsk.Wait();
Try:

Task.Run(() => AsyncOperation()).Wait();
Or if you need a result:

var result = Task.Run(() => AsyncOperation()).Result;