似乎放入Task.WhenAll()
一个try-catch
块不起作用。该代码应该将所有图像上传到ftp,但似乎如果一次上传失败(例如,图像文件在另一个进程中打开),整个uploadTasks
将停止,并且counts
是空的。
private async Task Upload(Ftp ftpHost)
{
var images = GetFileInfo() // will get a List<FileInfo>
var uploadTasks = images.Where(fi => fi.Exists).Select(async fi =>
{
var ret = await ftp.Upload(fi,_cancellationTokenSource.Token);
fi.Delete();
return ret;
});
IEnumerable<int> counts = new List<int>();
try
{
counts = await Task.WhenAll(uploadTasks);
}
catch
{
//ignore to allow all upload complete
}
var uploadedOrderFileByteCount = counts.Sum();
var uploadedOrderFileCount = counts.Count();
}