c# - how to allow all task complete when using Task.WhenAll()? -
it seems putting task.whenall()
in try-catch
block doesn't work. code should upload images ftp, seems if 1 upload fails (for example, image file open in process), whole uploadtasks
stop, , counts
empty.
private async task upload(ftp ftphost) { var images = getfileinfo() // 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 allow upload complete } var uploadedorderfilebytecount = counts.sum(); var uploadedorderfilecount = counts.count(); }
apart fact empty catch block bad idea (try catch exception ftp upload can throw especifically), if that's want do, easiest way catch inside operation itself, similar code below.
var uploadtasks = images.where(fi => fi.exists).select(async fi => { try { var ret = await ftp.upload(fi,_cancellationtokensource.token); fi.delete(); return ret; } catch { // again, please don't this... return 0; } }); ienumerable<int> counts = new list<int>(); try { counts = await task.whenall(uploadtasks); } catch { // bad happened, don't ignore }
Comments
Post a Comment