Pausing URLConnection

A web app uses a subclass of URLConnection to process a stack of sequential posts / puts / gets to another. Some time is needed between each send. What is the best way for the URLConnection subclass to wait a second before the next send?

Is code running in a method in a subclass of URLConnection on the main thread? If so how can I get the URLConnection to yield?

I get the impression that the subclass executes in the main thread but the connection won’t necessarily be ready for the next send if the URL connection subclass is referenced by a session - some kind of allowance for unfinished business perhaps. Is there no way to force the connection to be ready for the next send?

One way to do this is to make a class that behaves like a controller. Use AddHandler to direct the ContentReceived event to a method and in there you deal with the response and spin up a brand new socket, letting the previous one die off when it wants to. If you need to keep track of the sockets, just add an array to the class for storing them.

Thanks Greg. Presently the subclass calls a method on the super in which send is fired. Would there be anything in principle hindering timer.calllater in the subclass calling that method in the super to achieve the delay?

Besides making it very hard to debug, the amount of time that it takes for the resource to be released may not be fixed.

Rather than refactor I’m going to give core.timer.calllater a try. It looks to me like the release of a resource in this particular case is a content JSON object (which can be turned into a string to avoid a reference being passed that could tie up the connection, thus allowing the ContentReceived event to complete). The actual service I’m communicating with is Trello which seems to be rate limited anyway.

But if I were doing the project from scratch I would definitely consider your approach as it avoids the risk of unexpected operation errors. I’ve seen this when polling a URLConnection with a timer to get full access to the session with the incoming data from the main thread. I solved it by dealing with the passed objects in the polling timer which then calls another timer to restore program flow if further processing is required. This allows the passed object references from the main thread to die with the polling timer, which freed up the URLConnection.