C# 下载StringAsync多个URL维护顺序

C# 下载StringAsync多个URL维护顺序,c#,html,url,asynchronous,C#,Html,Url,Asynchronous,我需要下载多个HTML页面作为字符串,并将它们存储到一个列表中。但是需要按照我开始下载的顺序存储。目前,列表按下载完成的顺序排列。因此,下载完成后,其内容将添加到列表中。但是他们不一定按照开始的顺序完成。我有点纠结于如何将这些数据按启动顺序存储在列表中 这是我当前的代码,这将启动下载序列。URLList是一个URL列表,我需要下载的字符串与原始列表的顺序相同: cts = new CancellationTokenSource(); try { await AccessTheWebAsy

我需要下载多个HTML页面作为字符串,并将它们存储到一个列表中。但是需要按照我开始下载的顺序存储。目前,列表按下载完成的顺序排列。因此,下载完成后,其内容将添加到列表中。但是他们不一定按照开始的顺序完成。我有点纠结于如何将这些数据按启动顺序存储在列表中

这是我当前的代码,这将启动下载序列。URLList是一个URL列表,我需要下载的字符串与原始列表的顺序相同:

cts = new CancellationTokenSource();
try
{
    await AccessTheWebAsync(cts.Token);
    listBox1.DataSource = VideoList;
}
catch (Exception)
{
    MessageBox.Show("\r\nDownload failed.\r\n");
}
cts = null;
然后调用以下代码:

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<Task<string>> downloadTasksQuery = from url in URLList select ProcessURL(url, client, ct);

    List<Task<string>> downloadTasks = downloadTasksQuery.ToList();

    while (downloadTasks.Count > 0)
    {
        Task<string> 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...): <a href=\"") + 32;
        VideoList.Add(length.Substring(refPoint, length.IndexOf("\">", refPoint) - refPoint));
    }
}

async Task<string> ProcessURL(string url, HttpClient client, CancellationToken ct)
{
    HttpResponseMessage response = await client.GetAsync(url, ct);
    string urlContents = await response.Content.ReadAsStringAsync();
    return urlContents;
}

我相信这很简单。但我只需要下载的字符串与原始URLList列表的顺序相同。任何帮助都将不胜感激。

未经测试,但这应该可以工作

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)");

    var downloadTasks = new List<Task<Tuple<string, int>>();

    int index = 0;
    foreach(var url in URLList)
        downloadTasks.Add(ProcessURL(url, client, ct, index++));

    while (downloadTasks.Count > 0)
    {
        Task<Tuple<string, int>> firstFinishedTask = await Task.WhenAny(downloadTasks);

        downloadTasks.Remove(firstFinishedTask);

        Tuple<string, int> result = await firstFinishedTask;  //this is the data of the first finished download
        int refPoint = result.Item1.IndexOf("Download (Save as...): <a href=\"") + 32;
        VideoList.Add(length.Item1.Substring(refPoint, length.IndexOf("\">", refPoint) - refPoint));
    }
}

async Task<Tuple<string, int>> ProcessURL(string url, HttpClient client, CancellationToken ct, int index)
{
    HttpResponseMessage response = await client.GetAsync(url, ct);
    string urlContents = await response.Content.ReadAsStringAsync();
    return Tuple.Create(urlContent, index);
}

我尝试了你的代码,但出现了一些错误,我修复了1,但不确定如何修复另一个。我对C还是有点陌生。它的在线字符串长度=wait firstFinishedTask;我已经更新了代码。result.Item2包含下载任务的索引。