Calculation of the percentage of progress

I am using webclient for dowlnoad file. I calculate the percentage of progress as below

  • I know the size of the file (I read it from the database table) of the file that will be downloaded.

  • I rely on the BytesRecieved WebClient property to know all the bytes received at boot time.

  • Algorithm used by me double dProgress = (e.BytesReceived / FileSize)*100); to calculate the percentage of progress.

However, I am not getting the right percentage of progress to update the progress bar.

Is there any method for calculating the percentage of progress?

+7
source share
6 answers

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

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

Do something like this:

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

This is because / performs integer division when dividing two integers. But you don’t want it. Therefore, you convert one of the variables to double .

+17
source

If you handle the DownloadProgressChanged event while performing asynchronous loading, the args events already have ProgressPercentage , so there is no point in reinventing it. A contrived example:

 var client = new WebClient(); var reset = new ManualResetEvent(false); client.DownloadProgressChanged += (s, e) => Console.WriteLine("{0} percent complete", e.ProgressPercentage); client.DownloadFileCompleted += (s, e) => reset.Set(); client.DownloadFileAsync(new Uri("http://myfilepathhere.com"), "file.name"); //Block till download completes reset.WaitOne(); 
+3
source

BytesReceived and FileSize are most likely integers, so you need to calculate the progress like this:

 double dProgress = 100.0 * e.BytesReceived / FileSize; 
+1
source

The problem is that both numbers are integers. When you separate them, they become less than 1, so they are rounded to 0. Typecast them to make the result float:

 double dProgress = ((double)e.BytesReceived / FileSize)*100) 
+1
source

This is a very difficult problem with basic arithmetic and C # casting.

Decision

First save the result of integer division in a double variable. And then enter cast for the integer.

 int x = 5, y = 10, answer; double ansDouble; answer = (int)(x / y) * 100; //percentage calculation Console.WriteLine("percentage={0}", answer); //>output percentage=0 answer = (int)((double)x / y) * 100; //percentage calculation Console.WriteLine("percentage={0}", answer); //>output percentage=0 answer = (int)((double)x / (double)y) * 100; //percentage calculation Console.WriteLine("x={0}", answer); //>output percentage=0 answer = (int)(x/(double)y) * 100; //percentage calculation Console.WriteLine("x={0}", answer); //>output percentage=0 ansDouble = ((double)x / y) * 100; answer = (int)ansDouble; Console.WriteLine("percentage={0}", answer); //>output percentage=50 

Notes for notes

It turns out that x / y = 0, for any values ​​of x and y, if they are integers. We cannot solve this in one line using any combination of casting

+1
source

Try the following:

 WebClient Client = new WebClient(); Client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged); 

...

  void ProgressChanged(object sender, DownloadProgressChangedEventArgs e) { double bytesIn = double.Parse(e.BytesReceived.ToString()); double totalBytes = double.Parse(e.TotalBytesToReceive.ToString()); double percentage = bytesIn / totalBytes * 100; int percente = int.Parse(Math.Truncate(percentage).ToString()); progressBar.Value = percente; } 

Or just using the value of ProgressPercentage .

  void ProgressChanged(object sender, DownloadProgressChangedEventArgs e) { progressBar.Value = e.ProgressPercentage; } 
0
source

All Articles