0
  private async Task Upload(Ftp ftpHost)
     {
         //var assetImages is List<FileInfo>  
         var uploadedOrderFileByteCount = 0;
         var uploadedOrderFileCount = 0;

         foreach (var fi in assetImages.Where(fi => fi.Exists))
         {
             uploadedOrderFileCount++;

             var count = ftp.Upload(fi);

             uploadedOrderFileByteCount += await count;
             fi.Delete();
         }
     }
4

1 回答 1

1

您可以async使用以下方法进行简单的并行处理Task.WhenAll

private async Task Upload(Ftp ftpHost)
{
   //var assetImages is List<FileInfo>  

   var uploadTasks = assetImages.Where(fi => fi.Exists).Select(async fi =>
   {
      var ret = await ftp.Upload(fi);
      fi.Delete();
      return ret;
   }).ToArray();
   var counts = await Task.WhenAll(uploadTasks);
   var uploadedOrderFileByteCount = counts.Sum();
   var uploadedOrderFileCount = counts.Count();
}
于 2013-05-06T23:30:41.767 回答