c# - Tracking DownloadProgress with synchronous download -


basically this:

using (webclient wc = new webclient()) {     wc.downloadprogresschanged += (sender, args) =>         {             progress = (float) args.bytesreceived / (float) args.totalbytestoreceive;         };     wc.downloadfile(new uri(nolastsegment + file), path); } 

this doesnt work, because progress fired asynchronous downloads downloadfileasync.

usually you've got other thread ui thread if can show progress, maybe you've got console app or something. can use kind of wait handle , set when download completes.

using (var completedevent = new manualreseteventslim(false)) using (webclient wc = new webclient()) {     wc.downloadfilecompleted += (sender, args) =>      {         completedevent.set();     };     wc.downloadprogresschanged += (sender, args) =>     {         progress = (float) args.bytesreceived / (float) args.totalbytestoreceive;     };     wc.downloadfileasync(new uri(nolastsegment + file), path);     completedevent.wait(); } 

Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -