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
Post a Comment