Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more: (untagged)
Hi everyone!
Could someone please guide me in this problem?

I have a webservice that returns a byte array as follows:

byte[] BAReturn = myWebService.GetData();<br />


The returned data is about 100MB.
I'm supposed to do a progressbar that tracks the percentage of completion of the transfer of the byte array stream. So what I did was to create a timer and insider the timer, the code is

myProgressBar.Percent = (BAReturn.Length / SizeOfData) * 100;

where SizeOfData is the known size of the byte array data to be transfered.

Now the problem is that it always just goes to 100 immediately no matter how big the data is. Just that there is a long pause before the sudden change to 100. This means i can't use BAReturn.Length to track the transfer.

How can i go about tracking the progress of transfer?
Thanks a million!!!!
Posted

1 solution

I think your problem in integer division and thus rounding error.

This would do much better:

C#
// myProgressBar.Percent ...
myProgressBar.Value = (100 * BAReturn.Length) /SizeOfData;


To be completely sure, you can even do this:

C#
double percent = (100d * BAReturn.Length) /SizeOfData;
myProgressBar.Value = (int)System.Math.Round(percent);



—SA
 
Share this answer
 
v2
Comments
StephenTan 30-May-11 4:58am    
Thank you for your reply. The progressbar is not really the question, but to track the progress of the large return data from a function.
Sergey Alexandrovich Kryukov 30-May-11 15:07pm    
I answered to the question as you formulated it.
If you have something which is "really the question", consider asking it and get straight to the point.
Did my fix work for you? If so, please formally accept it (green button) and let's go ahead.
--SA
StephenTan 30-May-11 21:12pm    
Actually I'm not sure about the mechanism behind the function return of such a big data. It could be just returning an address after the whole data has been received, hence the sudden change to 100%. It has nothing to do with the rounding off.
Sergey Alexandrovich Kryukov 30-May-11 23:08pm    
What, not sure?! There is nothing to be sure about. Number of bytes number of bytes, nothing else. If you calculate it correctly. As do %, what you have written is just a bug.
Again, if you have a different concern, ask a different question.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900