Worker IO.IOException Using URLConnection

Trying to download a file in a worker JobRun event using this code:

Var HTTPConn As New URLConnection
Var dFile As FolderItem = SpecialFolder.UserHome.Child(“Downloads”).Child(“test.iso”)
HTTPConn.Send(“GET”, “http:// pathto.local /server /test.iso”, dFile)
*Spaces added to URL to prevent hyperlink.

Results with: Xojo.IO.IOException (The file could not be opened)

Interestingly the file does download just fine in a simulated worker console thread even though the debugger breaks on the exception. When I compile the app the helper creates the file in the specified location but does not download it.

Is there some kind of (threading) going on in the worker when it is compiled as a console app that could be causing this? Why am I getting the IOException? Thanks.

You probably need to use the synchronous version of the call. Otherwise the run event will complete and the worker will close and kill it off.

1 Like

Thanks for the synchronous suggestion but I really need to use the async version so I can use the ReceivingProgressed event via AddHandler to update a progress bar in the main UI. I sort of have it working now after taking another look at how JobComplete and JobRequested was indeed stopping it too early. This might be a job better suited for a separate console application instead of a worker. Thanks for your help.

Then don’t use a local variable. Make a property in the worker to prevent the URConnection from going out of scope.

2 Likes

This is the first time I have tried using the worker class. I resolved the IOException issues they were due to my misunderstanding of how the JobRequested event works with the multiple workers that are created based on core count and percent.

I still can’t get an async request to download a file in a custom URLConnection class in the compiled version but it works in the threaded debugger version.

I can’t access any properties in the worker from the JobRun console side. You have to pass them in through the return in JobRequested. Any other suggestions? Thanks.

What I am currently trying to do.

I have a custom URLConnection class called clsHTTPSock.
I have a custom Worker class called clsHTTPWorker.

In the JobRun event I create an instance of a custom worker class and pass the worker reference. Then I call a method in that class to send the request.

Var clsHTTPWorker As New clsHTTPWorker(strJobData, Self)
jobProgress = clsHTTPWorker.HTTPRequest(Self)

In the clsHTTPWorker Constructor I create the socket. HTTPSock is a property in clsHTTPWorker and it is defined to type clsHTTPSock.

HTTPSock = New clsHTTPSock

Then in clsHTTPWorker.HTTPRequest I send the file request

HTTPSock.Send(“GET”, strRequestURL, dFile)

I finally solved the async file download issue by forcing the worker to stay alive using a while loop in the JobRun event checking for a boolean indicating the download had finished.