Monday, 15 August 2011

winforms - download rate for a downloader using C# -



winforms - download rate for a downloader using C# -

i want implement download rate in downloader .after googling found rather complicated app download manager - limit download speed.then ,on thread how add together download rate , limit rate in downloader in c# found suggestion:

set timer fires every second, , utilize counter record how many bytes have been downloaded, study download rate x bytes/s in timer tick event, reset counter zero

just got on implementing this, using

bytesin = int.parse(e.bytesreceived.tostring()); totalbytes = int.parse(e.totalbytestoreceive.tostring());

i have bytesin in code showing bytesreceived yet how implement suggestion quoted above using timers if utilize tick() event , count on every tick() event won't show me down-speed.

suggestions please?

what need record bytesin during timer , store later. next time timer fires (one sec later) take new bytesin value subtract previous value. number of bytes downloaded in lastly second. show number on screen #### bytes/s split number 1024 , display result #### kbytes/s.

long lastdownload = 0; long bytesin = 0 //this event timer fires 1 time per sec on ui thread. private void timertick(object sender, eventargs e) { //figure out how many bytes downloaded in lastly second. var bytesdownloadedlastsecond = bytesin - lastsdownload; //copy number on next firing of timertick. lastdownload = bytesin; double scalingfactor; string formattext; //figure out if should display in bytes, kilobytes or megabytes per second. if(bytesdownloadedlastsecond < 1024) { formattext = "{0:n0} b/sec"; scalingfactor = 1; } else if(bytesdownloadedlastsecond < 1024 * 1024) { formattext = "{0:n2} kb/sec"; scalingfactor = 1024; } else { formattext = "{0:n2} mb/sec"; scalingfactor = 1024 * 1024; } //display speed user, scaled right size. speedtextbox.text = string.format(formattext, bytesdownloadedlastsecond / scalingfactor ); } private void downloadprogress(object sender, eventargs e) { bytesin = int.parse(e.bytesreceived.tostring()); totalbytes = int.parse(e.totalbytestoreceive.tostring()); }

c# winforms webclient-download

No comments:

Post a Comment