Download multiple files async and wait for all of them to finish before executing the rest of the code

后端 未结 1 745
执念已碎
执念已碎 2020-11-28 11:08

I am trying to download multiple files from the internet and await for all of them to finish. This is a C# console application that I am running, so no progress bar event h

相关标签:
1条回答
  • 2020-11-28 11:44

    The DownloadFileAsync/DownloadFileCompleted members of WebClient use the Event-based Asynchronous Pattern. If you want to use async and await, you should be using the Task-based Asynchronous Pattern.

    In this case, you should use the DownloadFileTaskAsync member, as such:

    private async Task DownloadFileAsync(DocumentObject doc)
    {
      try
      {
        using (WebClient webClient = new WebClient())
        {
          string downloadToDirectory = @Resources.defaultDirectory + doc.docName;
          webClient.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
          await webClient.DownloadFileTaskAsync(new Uri(doc.docUrl), @downloadToDirectory);
    
          //Add them to the local
          Context.listOfLocalDirectories.Add(downloadToDirectory);
        }         
      }
      catch (Exception)
      {
        Errors.printError("Failed to download File: " + doc.docName);
      }
    }
    
    private async Task DownloadMultipleFilesAsync(List<DocumentObject> doclist)
    {
      await Task.WhenAll(doclist.Select(doc => DownloadFileAsync(doc)));
    }
    

    Please note that your Context.listOfLocalDirectories.Add and Errors.printError methods should be threadsafe.

    0 讨论(0)
提交回复
热议问题