简体   繁体   中英

Async methods doesn't seem to work on Windows Phone

I have the following piece of code (WPF, Windows Phone 8.1):

HttpClient client = new HttpClient();
var httpResult = client.GetAsync(feed.Url, ct);
string feedData = await httpResult.Result.Content.ReadAsStringAsync();

var sf = new SyndicationFeed();
sf.Load(feedData);

I'm trying to debug this code. However, after the line:

string feedData = await httpResult.Result.Content.ReadAsStringAsync();

debugger seems to let application run on its own and never reaches the next line. Why is that? Am I doing something wrong?

Depending on if you are calling result or wait on the task somewhere upstream, this can result in a deadlock as noted in Stephen Cleary's blog post .

Mitigate this by awaiting the client.GetAsync() and use ConfigureAwait where possible to minimize chances of deadlocks:

HttpClient client = new HttpClient();
var httpResult = await client.GetAsync(feed.Url, ct).ConfigureAwait(false);
string feedData = await httpResult.Content.ReadAsStringAsync().ConfigureAwait(false);

var sf = new SyndicationFeed();
sf.Load(feedData)

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