I’m trying to download and parse a website using the async functions for a Windows 8 app.
I call the download code in this method:
private async Task<Show> getSeasonAndEpisodeInformation(Show currentShow)
{
int seriesID = currentShow.SeriesID;
EpisodeBuilder epBuilder = new EpisodeBuilder(seriesID);
List<Episode> eps = await epBuilder.getEpisodeList();
SeasonBuilder seasonBuilder = new SeasonBuilder(eps);
return currentShow;
}
And the getEpisodeList() function is:
public async Task<List<Episode>> getEpisodeList()
{
httpClient = new HttpClient();
httpClient.MaxResponseContentBufferSize = 2560000;
httpClient.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");
try
{
List<Episode> episodes = new List<Episode>();
HttpResponseMessage response = await httpClient.GetAsync(new Uri("http://www.example.com"));
response.EnsureSuccessStatusCode();
//Never get here
return episodesList;
}
catch (Exception)
{
}
return null;
}
If I have the getEpisodeList method be void then the webpage will download and continue. I have debugged and made sure that it is the httpClient.GetAsync(..) line that is never completing. Is there anyway around this?
In the getSeasonAndEpisodeInformation method, I need to make sure that the getEpisodeList() method has returned before continuing – hence the use of the await keyword when calling epBuilder.getEpisodeList().
I fixed this by removing the “await” keyword when I call http.GetAsync. The call now finishes and the ‘await’ in the getSeasonAndEpisodeInformation method ensures that it runs asynchronously.