just a quick question. We have some misunderstanding here.
we have:
var tasks = files.Select(async fileName => await IngestFileAsync(container, fileName)); var results = await Task.WhenAll(tasks);
I say that the first line still goes in parallel, but my colleague says differently. In addition, he says that the second await does not make sense, because all the actions have already been completed.
is the same code:
var tasks = files.Select(fileName => IngestFileAsync(container, fileName)); var results = await Task.WhenAll(tasks);
as:
var tasks = files.Select(async fileName => await IngestFileAsync(container, fileName)); var results = Task.WhenAll(tasks);
Can anyone shed some light on this?
greetings.
Added: oke, so it will work at the same time.
However, someone can add additional information about what is the difference between these code snippets: https://dotnetfiddle.net/lzv2B7 https://dotnetfiddle.net/dMusus
(notification line 16, async and await ). Is there any difference between these 2? I would expect that with async and wait for it to start directly, but without it it will start when it comes to Await Task.WhenAll(tasks);
added for clarity - this is my code -:
private async Task<Result> IngestFilesAsync(ICloudBlobContainer container, IEnumerable<string> files) { _logger.LogDebug("Start IngestFilesAsync"); var tasks = files.Select(fileName => IngestFileAsync(container, fileName)); var results = await Task.WhenAll(tasks); _logger.LogDebug("All tasks completed"); if (results.Any(t => t.IsFailure)) { return Result.Fail(string.Join(",", results.Select(f => f.Error))); } return Result.Ok(); } private async Task<Result> IngestFileAsync(ICloudBlobContainer container, string fileName) { _logger.LogDebug("Start IngestFileAsync"); var blob = container.GetBlockBlobReference(fileName); _logger.LogDebug("Blob retrieved"); if (await blob.ExistsAsync()) { using (var memoryStream = new MemoryStream()) { _logger.LogDebug("Start download to stream"); await blob.DownloadToStreamAsync(memoryStream); _logger.LogDebug("To mem downloaded"); _logger.LogDebug("Start ftp-upload"); return await _targetFTP.UploadAsync(memoryStream, fileName); } } _logger.LogWarning("Blob does not exists"); return Result.Fail($"Blob '{fileName}' does not exist"); }
where _targetFTP.UploadAsync(memoryStream, fileName); task again, etc. etc.