Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating the progress percentage

Tags:

c#

I am using webclient to dowlnoad a file. I am calculating the progress percentage as below

  1. I know the filesize (I read it from the database table) of the file going to be downloaded.

  2. I am depending on the BytesRecieved property of WebClient to know the total bytes fetched during download.

  3. The algorithm I am using is double dProgress = (e.BytesReceived / FileSize)*100); to calculate the progress percentage.

However i am not getting correct progress percentage to update the progress bar.

Is there any method to calculate progress percentage?

like image 463
logeeks Avatar asked Aug 27 '11 21:08

logeeks


2 Answers

Look at the following line: double dProgress = (e.BytesReceived / FileSize)*100)

If both e.BytesReceived and FileSize are integers then you will always have 0 * 100 = 0.

Make something like this:

double dProgress = ((double)e.BytesReceived / FileSize)*100.0

It is because / does integer division when dividing two integers. But you don't want that. So you convert one of the variables to double.

like image 164
Petar Minchev Avatar answered Sep 20 '22 09:09

Petar Minchev


BytesReceived and FileSize most probably are integers so you need to calculate progress this way:

double dProgress = 100.0 * e.BytesReceived / FileSize;
like image 23
Andrey Kamaev Avatar answered Sep 22 '22 09:09

Andrey Kamaev