异步方法似乎不适用于Windows Phone

时间:2015-01-09 19:46:47

标签: c# asynchronous windows-phone-8.1

我有以下代码(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);

我试图调试此代码。然而,在行之后:

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

调试器似乎让应用程序自行运行,永远不会到达下一行。这是为什么?我做错了吗?

1 个答案:

答案 0 :(得分:2)

根据您是在呼叫结果还是在上游某处等待任务,这可能会导致陷入僵局,如Stephen Cleary的blog post所述。

通过等待client.GetAsync()来缓解此问题,并尽可能使用ConfigureAwait以尽量减少死锁的可能性:

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)