Note that there are some explanatory texts on larger screens.

plurals
  1. PODownloadStringAsync multiple URLs maintaining order
    text
    copied!<p>I need to download multiple HTML pages as strings and store them into a list. But then need to be stored in the order i started the downloads. At the moment the list is in the order of download completion. So when a download finishes, its contents gets added to a list. But they dont necessarily finish in the order they were started. And im kinda stumped on how to store this data in a list in the order they were initiated.</p> <p>Here is my current code, this initiates the download sequence. URLList is a list of urls, and i need the downloaded strings to be in the same order as this original list:</p> <pre><code>cts = new CancellationTokenSource(); try { await AccessTheWebAsync(cts.Token); listBox1.DataSource = VideoList; } catch (Exception) { MessageBox.Show("\r\nDownload failed.\r\n"); } cts = null; </code></pre> <p>Then that calls the following code:</p> <pre><code>async Task AccessTheWebAsync(CancellationToken ct) { HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)"); IEnumerable&lt;Task&lt;string&gt;&gt; downloadTasksQuery = from url in URLList select ProcessURL(url, client, ct); List&lt;Task&lt;string&gt;&gt; downloadTasks = downloadTasksQuery.ToList(); while (downloadTasks.Count &gt; 0) { Task&lt;string&gt; firstFinishedTask = await Task.WhenAny(downloadTasks); downloadTasks.Remove(firstFinishedTask); string length = await firstFinishedTask; //this is the data of the first finished download int refPoint = length.IndexOf("Download (Save as...): &lt;a href=\"") + 32; VideoList.Add(length.Substring(refPoint, length.IndexOf("\"&gt;", refPoint) - refPoint)); } } async Task&lt;string&gt; ProcessURL(string url, HttpClient client, CancellationToken ct) { HttpResponseMessage response = await client.GetAsync(url, ct); string urlContents = await response.Content.ReadAsStringAsync(); return urlContents; } </code></pre> <p>Im sure its something rather simple. But i just need the downloaded strings to be in the same order as the original URLList list. Any help would be much appreciated.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload