I have an app that making API requests to the last.fm website using the backgroundWorker. Initially I don't know how many request I'm gonna need to make. The response contains the total number of pages, so I will only get it after the first request. Here is the code below.
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
int page = 1;
int totalpages = 1;
while (page <= totalpages)
{
if (backgroundWorker.CancellationPending)
{
e.Cancel = true;
return;
}
//Here is the request part
string Response = RecentTracksRequest(username, from, page);
if (Response.Contains("lfm status=\"ok"))
{
totalpages = Convert.ToInt32(Regex.Match(Response, @"totalPages=.(\d+)").Groups[1].Value);
MatchCollection match = Regex.Matches(Response, "<track>((.|\n)*?)</track>");
foreach (Match m in match)
ParseTrack(m.Groups[1].Value);
}
else
{
MessageBox.Show("Error sending the request.", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (page >= totalpages)
break;
if (totalpages == 0)
break;
if (page < totalpages)
page++;
}
The problem is that the last.fm API is really slow and it might take up to 5 seconds to get the responce. With a big number of pages, the loading will take a long time.
I want to make parallel requests, say 3 parallel requests at a time. Is it possible? If yes, how can I make it?
Thanks a lot.