异步方法似乎不适用于 Windows Phone
Async methods doesn't seem to work on Windows Phone
我有以下代码(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();
调试器似乎让应用程序 运行 自行运行,永远不会到达下一行。这是为什么?我做错了什么吗?
根据您是调用结果还是在上游某处等待任务,这可能会导致死锁,如 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)
我有以下代码(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();
调试器似乎让应用程序 运行 自行运行,永远不会到达下一行。这是为什么?我做错了什么吗?
根据您是调用结果还是在上游某处等待任务,这可能会导致死锁,如 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)