简体   繁体   English

查找并行异步任务的结果

[英]Find Result of Parallel Async Tasks

Based off this question I'm trying to set up code to save several images to Azure Blob Storage in parallel. 基于这个问题,我正在尝试设置代码以将多个图像并行保存到Azure Blob存储。 This method below works fine and awaiting Task.WhenAll(tasks) awaits for all to complete before continuing. 下面这个方法工作正常,等待Task.WhenAll(任务)等待所有人完成后再继续。

The only trouble is, I would like to be able to find out if each request to store the information in our database actually succeeded. 唯一的麻烦是,我希望能够找出每个在我们的数据库中存储信息的请求是否真的成功了。 _db.AddImageAsync returns a bool and the code below waits for all tasks to complete but when I check the result of all the tasks each is false (even if I actually returned true inside the brackets). _db.AddImageAsync返回一个bool,下面的代码等待所有任务完成,但是当我检查所有任务的结果时,每个都是假的(即使我在括号内实际返回true)。

Each task in the Enumerable says the result has not yet been computed even though I stepped through with breakpoints and each has been carried out. Enumerable中的每个任务都表示,即使我逐步执行断点并且每个任务都已执行,结果仍未计算。

  var tasks = wantedSizes.Select(async (wantedSize, index) =>
  {
    var resize = size.CalculateResize(wantedSize.GetMaxSize());
    var quality = wantedSize.GetQuality();

    using (var output = ImageProcessHelper.Process(streams[index], resize, quality))
    {
        var path = await AzureBlobHelper.SaveFileAsync(output, FileType.Image);
        var result = await _db.AddImageAsync(id, wantedSize, imageNumber, path);
        return result;
    }
  });

  await Task.WhenAll(tasks)

  if (!tasks.All(task => task.Result))
      return new ApiResponse(ResponseStatus.Fail);

Any help is much appreciated! 任何帮助深表感谢!

Because .Select( is lazy evaluated and returns a IEnumerable<Task<bool>> you are causing the .Select( to be run multiple times when you iterate over the result multiple times. Throw a .ToList() on it to make it a List<Task<bool>> and that will only execute the .Select( once and the multiple enumerations will be over the returned List<Task<bool>> which will not have side effects. 因为.Select(延迟求值并返回一个IEnumerable<Task<bool>>你导致.Select(当你多次遍历结果时多次运行。在它上面抛出一个.ToList()使它成为一个List<Task<bool>>并且只执行.Select(一次,多次枚举将在返回的List<Task<bool>> ,这将没有副作用。

  var tasks = wantedSizes.Select(async (wantedSize, index) =>
  {
    var resize = size.CalculateResize(wantedSize.GetMaxSize());
    var quality = wantedSize.GetQuality();

    using (var output = ImageProcessHelper.Process(streams[index], resize, quality))
    {
        var path = await AzureBlobHelper.SaveFileAsync(output, FileType.Image);
        //Double check your documentation, is _db.AddImageAsync thread safe?
        var result = await _db.AddImageAsync(id, wantedSize, imageNumber, path);
        return result;
    }
  }).ToList(); //We run the Select once here to process the .ToList().

  await Task.WhenAll(tasks) //This is the first enumeration of the variable "tasks".

  if (!tasks.All(task => task.Result)) //This is a 2nd enumeration of the variable.
      return new ApiResponse(ResponseStatus.Fail);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM