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

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -