Fish_Guts182

Hello,

I've made a small piece of program that upload 2 files (one after the other).
I use UploadFileAsync with a WebClient.

I use also a progressBar, to indicate the progression, for the first file it's OK, but for the second file, progressPercentage give me a wrong number. In fact, it's the actual value divided by 2 !

The upload function

Code Block

private void wc_sendFile(Uri uri, String file)
{
//MessageBox.Show(Convert.ToString(fichierEnCours));
wc = new WebClient();
wc.UploadProgressChanged += new UploadProgressChangedEventHandler(wc_UploadProgressChanged);
wc.UploadFileCompleted += new UploadFileCompletedEventHandler(wc_UploadFileCompleted);
wc.Credentials = new NetworkCredential("****","*******");
wc.UploadFileAsync(uri, file);
wc.Dispose();
}



The UploadProgress Event Handler :

Code Block

private void wc_UploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}



And, of course, progressBar1.Maximum = 100;


While debbuging,

BytesSent 1155072 long

TotalBytesToSend 2300290 long

ProgressPercentage 25 int





Can you explain me this please


Re: Visual C# Express Edition Wrong Progresspercentage ?

Derek Smyth

Hi,

I don't know if this is the cause of the problem but....

These two lines of code....

wc.UploadFileAsync(uri, file); //doesn't block
wc.Dispose();

Your disposing the WebClient object while the asynchronous upload is still happening.




Re: Visual C# Express Edition Wrong Progresspercentage ?

Fish_Guts182

I'm quite noob -_-


I'll check what is really dispose. Thanks for the advice Smile


ok, line moved. (and it's not fixing my problem, I think it's quite normal).




Re: Visual C# Express Edition Wrong Progresspercentage ?

Derek Smyth

Hi again,

Are you running your upload against the localhost

I've tried it out, using the localhost, and I'm getting very odd results (i.e. 34% after 50%, sometimes 34% appears after upload is finished) but I think it's happening like this because I'm running it locally and uploading to localhost which is almost a direct file copy so the progress events are firing at the same time and handled in a different order than they are raised.






Re: Visual C# Express Edition Wrong Progresspercentage ?

Fish_Guts182

Hello,

Nope, I'm uploading to a remote server.

What's strange is the first upload runs fine, progresspercent is OK, and for the second file, the percent is exactly divided by two.

I've fixed it temporarly by fixing Maximum at 50 for the 2nd upload.

The file is fully uploaded, but percenprogress tells "50%" ...




Re: Visual C# Express Edition Wrong Progresspercentage ?

Derek Smyth

Hi,

Yip, I'm consistantly getting funny results as well.....

0% completed: 151 send, 12218 total
49% completed: 12174 send, 12218 total <- dodgy
34% completed: 8343 send, 12218 total
50% completed: 12218 send, 12218 total <- well dodgy
100% completed: 12218 send, 12218 total
100% completed: 12218 send, 12218 total
Upload Complete

I've got no idea! Only other thing I can suggest apart from what your doing is to calculate your own percentage.