Hey,
a few days ago I started working on a download program, it's pretty good so far imo.
Now I want to add a new feature, showing the download speed in KB/s but I have absolutely no idea how to do this, can anyone please help me out?
-Simon
Printable View
Hey,
a few days ago I started working on a download program, it's pretty good so far imo.
Now I want to add a new feature, showing the download speed in KB/s but I have absolutely no idea how to do this, can anyone please help me out?
-Simon
The actual implementation depends upon how you are downloading. Here's a way if you are downloading iteratively (eg you are using loops to download). Create a Timer that fires however often you wish to update the download speed in the GUI. Have a variable that holds the current download size and set it every run through the loop. Every time the timer fires subtract the previous size from the current size, and divide this by the number of seconds since the previous firing == bytes/s.
Code :int currentAmount = 0;//set this during each loop of the download /***/ int previousAmount = 0; int firingTime = 1000;//in milliseconds, here fire every second public synchronyzed void run(){ int bytesPerSecond = (currentAmount-previousAmount)/(firingTime/1000); //update GUI using bytesPerSecond previousAmount = currentAmount; }