HTTPSocket with ProgressBar

I have a window for downloading files that has an HTTPSocket and a ProgressBar.

When compiled for MacOS everything will work just fine, but in Windows the ProgressBar will give an NilObjectException when the ProgressBar tries to display the download progress. If I remove the progressBar the download will complete without an error.

The error occurs in the ReceiveProgress event of the HTTPSocket. The code is:

[code]Sub ReceiveProgress(bytesReceived as integer, totalBytes as integer, newData as string)
Dim pct As Integer = (bytesReceived / totalBytes) * 100

ProgressBar1.Value = pct
End Sub[/code]

Anyone know what the problem maybe?

thanks.

[quote=194471:@Malcolm Wooden]I have a window for downloading files that has an HTTPSocket and a ProgressBar.

When compiled for MacOS everything will work just fine, but in Windows the ProgressBar will give an NilObjectException when the ProgressBar tries to display the download progress. If I remove the progressBar the download will complete without an error.

The error occurs in the ReceiveProgress event of the HTTPSocket. The code is:

[code]Sub ReceiveProgress(bytesReceived as integer, totalBytes as integer, newData as string)
Dim pct As Integer = (bytesReceived / totalBytes) * 100

ProgressBar1.Value = pct
End Sub[/code]

Anyone know what the problem maybe?

thanks.[/quote]

NilObjectException does not look like the kind of error that could come from Pct.

Looks more like for some reason ProgressBar1 could come up nil.

When the error occurs you get the debugger at the bottom of the code that shows the variables. Look in there to see what is nil.

Hi Michel

I agree with you that the error is not from the pct variable. I removed the progress bar and replaced it with a Label that updates with the percentage of download. This again worked on Mac but not on Windows.

The error seems to be in the HTTPSocket class as the bytesRecieved event and error event never happen.

[quote=194542:@Malcolm Wooden]Hi Michel

I agree with you that the error is not from the pct variable. I removed the progress bar and replaced it with a Label that updates with the percentage of download. This again worked on Mac but not on Windows.

The error seems to be in the HTTPSocket class as the bytesRecieved event and error event never happen.[/quote]

I gather the label does not trigger a nil object exception ?

Can you try to place this at the top of the event. It should display the values in the message pane of the IDE :

System.Debuglog str(bytesReceived)+"/"+str(totalBytes)

The Label acts in the same way as the progress bar, I don’t think it gets as far as acting on it.

I placed your code at the top of the ReceiveProgress event but nothing was returned from it. I guess that means that the HTTPSocket is not getting to that event.

[quote=194548:@Malcolm Wooden]The Label acts in the same way as the progress bar, I don’t think it gets as far as acting on it.

I placed your code at the top of the ReceiveProgress event but nothing was returned from it. I guess that means that the HTTPSocket is not getting to that event.[/quote]

How did you add HTTPSocket to the project, and then how did you add the event handler to HTTPSocket ?

Did you create a class with super HTTPSocket and add to it the event, or did you add HTTPSocket as a property like in the Xojo example, and the ReceiveProgress event through AddHandler ?

I just verified the sample project at Example Projects\Communication\Internet\HTTP Example.xojo_binary_project does work perfectly in a setup similar to what you describe, using ReceiveProgress to push a ProgressBar. Try to download a file long enough to show the progressbar, with the central button.

Maybe you want to study it to apply the same approach.

I have based it on that Xojo example. I confused as to why it works on MacOS/X but not on Windows.

I did test and it runs just fine under Windows. You must check what you may have modified that explains why something goes nil. Have you used the same WebSocket class ?

I do the same using certainly the same example and I never had this trouble.

Note: I did not understand how the ProgressBar worked (in the example), but since it worked fine in both OS X and Windows…

Also: the progress bar look is different from OS X and Windows (8.1) ;-:slight_smile:

Solved it!

The difference between my project and the example was that my download was triggered by the Windows ‘open’ event and not directly from a button control, and for some reason Windows does not like that.

I moved the code from the ‘open’ event into a single shot timer and …presto!

thanks