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

html - jquery - p element wont show after I hid it -

python - BeautifulSoup: How to get the nearest tag -

mysql - Trying to get RmySQL to work but not understanding bash's export or filesystem conventions -