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

qml - Is it possible to implement SystemTrayIcon functionality in Qt Quick application -

double exclamation marks in haskell -

javascript - How to get D3 Tree link text to transition smoothly? -