My aim is to download images from an Amazon Web Services bucket.
I have the following code function which downloads multiple images at once:
public static void DownloadFilesFromAWS(string bucketName, List<string> imageNames) { int batchSize = 50; int maxDownloadMilliseconds = 10000; List<Task> tasks = new List<Task>(); for (int i = 0; i < imageNames.Count; i++) { string imageName = imageNames[i]; Task task = Task.Run(() => GetFile(bucketName, imageName)); tasks.Add(task); if (tasks.Count > 0 && tasks.Count % batchSize == 0) { Task.WaitAll(tasks.ToArray(), maxDownloadMilliseconds);//wait to download tasks.Clear(); } } //if there are any left, wait for them Task.WaitAll(tasks.ToArray(), maxDownloadMilliseconds); } private static void GetFile(string bucketName, string filename) { try { using (AmazonS3Client awsClient = new AmazonS3Client(Amazon.RegionEndpoint.EUWest1)) { string key = Path.GetFileName(filename); GetObjectRequest getObjectRequest = new GetObjectRequest() { BucketName = bucketName, Key = key }; using (GetObjectResponse response = awsClient.GetObject(getObjectRequest)) { string directory = Path.GetDirectoryName(filename); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } if (!File.Exists(filename)) { response.WriteResponseStreamToFile(filename); } } } } catch (AmazonS3Exception amazonS3Exception) { if (amazonS3Exception.ErrorCode == "NoSuchKey") { return; } if (amazonS3Exception.ErrorCode != null && (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity"))) { // Log AWS invalid credentials throw new ApplicationException("AWS Invalid Credentials"); } else { // Log generic AWS exception throw new ApplicationException("AWS Exception: " + amazonS3Exception.Message); } } catch { // } } The downloading of the images all works fine but the Task.WaitAll seems to be ignored and the rest of the code continues to be executed - meaning I try to get files that are currently non existent (as they've not yet been downloaded).
I found this answer to another question which seems to be the same as mine. I tried to use the answer to change my code but it still wouldn't wait for all files to be downloaded.
Can anyone tell me where I am going wrong?