After looking at this blog reader tutorial I noticed that there are 14 asynchronous calls being made within a single method. Are these all run at the same time or in parallel? If not, then what happens when an error occurs? Do the rest run serially, or is the entire method stopped?
Example code:
public async Task GetFeedsAsync()
{
Task<FeedData> feed1 =
GetFeedAsync("http://windowsteamblog.com/windows/b/developers/atom.aspx");
Task<FeedData> feed2 =
GetFeedAsync("http://windowsteamblog.com/windows/b/windowsexperience/atom.aspx");
Task<FeedData> feed3 =
GetFeedAsync("http://windowsteamblog.com/windows/b/extremewindows/atom.aspx");
Task<FeedData> feed4 =
GetFeedAsync("http://windowsteamblog.com/windows/b/business/atom.aspx");
Task<FeedData> feed5 =
GetFeedAsync("http://windowsteamblog.com/windows/b/bloggingwindows/atom.aspx");
Task<FeedData> feed6 =
GetFeedAsync("http://windowsteamblog.com/windows/b/windowssecurity/atom.aspx");
Task<FeedData> feed7 =
GetFeedAsync("http://windowsteamblog.com/windows/b/springboard/atom.aspx");
Task<FeedData> feed8 =
GetFeedAsync("http://windowsteamblog.com/windows/b/windowshomeserver/atom.aspx");
// There is no Atom feed for this blog, so we use the RSS feed.
Task<FeedData> feed9 =
GetFeedAsync("http://windowsteamblog.com/windows_live/b/windowslive/rss.aspx");
Task<FeedData> feed10 =
GetFeedAsync("http://windowsteamblog.com/windows_live/b/developer/atom.aspx");
Task<FeedData> feed11 =
GetFeedAsync("http://windowsteamblog.com/ie/b/ie/atom.aspx");
Task<FeedData> feed12 =
GetFeedAsync("http://windowsteamblog.com/windows_phone/b/wpdev/atom.aspx");
Task<FeedData> feed13 =
GetFeedAsync("http://windowsteamblog.com/windows_phone/b/wmdev/atom.aspx");
Task<FeedData> feed14 =
GetFeedAsync("http://windowsteamblog.com/windows_phone/b/windowsphone/atom.aspx");
this.Feeds.Add(await feed1);
this.Feeds.Add(await feed2);
this.Feeds.Add(await feed3);
this.Feeds.Add(await feed4);
this.Feeds.Add(await feed5);
this.Feeds.Add(await feed6);
this.Feeds.Add(await feed7);
this.Feeds.Add(await feed8);
this.Feeds.Add(await feed9);
this.Feeds.Add(await feed10);
this.Feeds.Add(await feed11);
this.Feeds.Add(await feed12);
this.Feeds.Add(await feed13);
this.Feeds.Add(await feed14);
}
The HttpClient asynchronous API of .NET 4.5 (used in WinRT) starts web request tasks automatically as soon as you call the HttpClient.GetAsync method (or their put, post delete etc.. équivalents).
These tasks are promises.
Therefore, in the example above, all the Tasks are started in parallel, then the thread will be made available by the first await until the first request return, then until the second returns, etc…
If a task is already completed when it’s awaited, the await instruction will run synchronously.
If an error occurs during the execution of a request, the exception will be thrown when the task is awaited. As a consequence, if you don’t await your task any exception will be lost.