Doing a URLConnection during App.CancelClose fails

I have an app that uses URLConnection to retrieve data from a server when it launches, and I need it to use URLConnection to post data back to the server before it quits.

So here’s what I’m trying to do: I added a PostClosingData method inside App.CancelClose:

gOKToCloseApp=False 'This is a global boolean


'PostClosingData creates an instance of a URLConnection and does a POST, then when the instance of the URLConnection gets a reply from the server that it got the data, it sets gOKToCloseApp=False

Loop Until gOKToClose=True

'And we exit App.CancelClose

I added debug logging entries, PostClosingData executes the POST, but it never receives a reply. If I put PostClosingData into a button’s action event, it works fine.

Clearly, there is some fundamental rule about the flow of events that I’m not grasping. My guess is that you can’t have events happening during an event (such as App.CancelClose)?

Any help gratefully appreciated!

  • John

I’m guessing you’re using URLConnection asynchronously if you’re do-looping in CancelClose. If so you could have a Boolean named SafeToQuit and when its False, actually return True from CancelClose to prevent the quit. The event handlers for the URLConnection could set SafeToQuit to True then call Quit allowing the final closure of the software.

It also occurred to me that I could just make it a .SendSync, though it could mean getting stuck if the server isn’t available or the connection is lost.

You might also face the more adept users complaining that the app hangs when you quit. In Lifeboat, I do a similar thing to nicely disconnect from the server.

Since both the CancelClose event and the URLConnection run on the main thread, your loop is blocking the URLConnection from running. In the past, you could .Poll the socket inside the loop, but I don’t see how to do that with URLConnection. One option would be to set a flag that tells the URLConnection to Quit the app and return True from CancelClose. That would allow the app to continue, the URLConnection to complete, and then the app to quit when it’s complete.

I was thinking it must be something like this. I can either use the .SendSync method, but your idea (which is very similar to Tim Parnell’s) sounds like a great solution.

Thank you!

  • John