我正在使用Parallel.ForEach
将 C# 中的多个文件从谷歌存储桶下载到文件夹位置。我正在使用重试逻辑,因此它可以在下载过程中文件下载失败的情况下重试下载文件。如何为每个文件或Parallel.ForEach
循环中的每个线程应用重试逻辑。
Parallel.ForEach(listFiles, objectName =>
{
retryCount = 0;
countOfFiles++;
downloadSuccess = false;
bucketFileName = Path.GetFileName(objectName.Name);
guidFolderPath = tempFolderLocation + "\\" + bucketFileName;
while (retryCount < retryCountInput && downloadSuccess == false)
{
try
{
FileStream fs = new FileStream(guidFolderPath, FileMode.Create, FileAccess.Write, FileShare.Write);
using (fs)
{
storage.DownloadObjectAsync(bucketName, objectName.Name, fs, option, cancellationToken, progress).Wait();
}
}
catch (Exception ex)
{
Console.WriteLine("Exception occured while downloading file: " + ex.ToString());
Thread.Sleep(RetryInterval(retryCount, minBackoffTimeSpan, maxBackoffTimeSpan, deltaBackoffTimeSpan));
retryCount++;
}
}
}