c# - Why do some files result in 0KB when using WebClient.DownloadFileTaskAsync -


i'm trying download multiple files ftp server using webclient.downloadfiletaskasync , repeatedly have issue several files end being 0kb.

i've tried different suggested solutions don't manage files. doing wrong?

class program {     static void main()     {         setup(); // sets working folders etc          task t = processasync();         t.continuewith(bl =>         {             if (bl.status == taskstatus.rantocompletion)                 logger.info("all done.");             else                 logger.warn("something went wrong.");         });         t.wait();     }      private static void setup() {...}      static async task<bool> processasync()     {         var c = new catalog();          var maxitems = settings.getint("maxitems");         logger.info((maxitems == 0 ? "processing items" : "processing first {0} items"), maxitems);          await c.processcatalogasync(maxitems);         return true; // not used atm     } }  public class catalog {     public async task processcatalogasync(int maxitems)     {         var client = new client();         var d = await client.getfoldersasync(_remotefolder, maxitems);         var list = d ilist<string> ?? d.tolist();         logger.info("found {0} folders", list.count());          await processfoldersasync(list);     }      private async task processfoldersasync(ienumerable<string> list)     {         var client = new client();         foreach (var mfolder in list.select(folder => _folder + "/" + folder))         {             var items = await client.getitemsasync(mfolder);             var file = items.firstordefault(n => n.tolower().endswith(".xml"));              if (string.isnullorempty(file))             {                 logger.warn("no metadata file found in {0}", mfolder);                 continue;             }              await client.downloadfileasync(mfolder, file);              // continue processing received file...         }     } }  public class client {     public async task<ienumerable<string>> getitemsasync(string subfolder)     {         return await getfolderitemsasync(subfolder, false);     }      public async task<ienumerable<string>> getfoldersasync(string subfolder, int maxitems)     {         var folders = await getfolderitemsasync(subfolder, true);         return maxitems == 0 ? folders : folders.take(maxitems);     }      private async task<ienumerable<string>> getfolderitemsasync(string subfolder, bool onlyfolders)     {         // downloads folder contents using webrequest     }      public async task downloadfileasync(string path, string file)     {         var remote = new uri("ftp://" + _hostname + path + "/" + file);         var local = _workingfolder + @"\" + file;         using (var ftpclient = new webclient { credentials = new networkcredential(_username, _password) })         {             ftpclient.downloadfilecompleted += (sender, e) => downloadfilecompleted(sender, e, file);             await ftpclient.downloadfiletaskasync(remote, local);         }     }      public void downloadfilecompleted(object sender, asynccompletedeventargs e, uri remote, string local)     {         if (e.error != null)         {             logger.warn("failed downloading\n\t{0}\n\t{1}", file, e.error.message);             return;         }         logger.info("downloaded \n\t{1}", file);     } } 

seems of tasks aren't completed. try (i same when putting bunch of files ftp)

  1. create array of tasks every file being downloaded. run them in cycle this:

    task[] tarray = new task[dicttoupload.count]; foreach (var pair in dicttoupload) {     tarray[i] = task.factory.startnew(()=>{/* stuff */}); } await task.whenall(tarray); 
  2. use await task.whenall(taskarray) instead of await each task. guarantees tasks completed.

  3. learn peace of tpl ;)


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -