Canceling a URL download


#1

Is there any way to signal a download via URL::createInputStream (or another JUCE way to download data) to cancel the download and return?
I noticed OpenStreamProgressCallback, but this seems to rather apply to the upload and also didn’t get called when I tried to use it (maybe I did something wrong).

The problem originates as I’m downloading something in a separate thread (to avoid the GUI being blocked) but as I can’t check for threadShouldExit() during the URL download I need to add a stopThread(timeOutURL) in the thread’s destructor. Due to this, the Application might take up to timeOutURL ms between clicking the close button and being really closed which I’d like to avoid, resulting in the above question.

Thanks
Chris


#2

From the lack of replies I assume this is either not possible, ridiculously simple or my approach is completely wrong.


#3

If you just destroy the inputstream that you’ve created, it should close the stream. Only problem is, I 'm not sure what timeout applies there and how it might vary across different streams.

Note also that the URL unit tests I wrote (http://www.juce.com/forum/topic/suggested-code-handling-url-redirects) show that the actual timeout generally takes longer than the specified connection timeout.


#4

Thanks for the input. As far as I understand at least on Windows and for smaller files, the url is completely read during the URL::createInputStream() call.If I set a breakpoint and disconnect the network after this line the content is still read.

If I find some time I’ll have a deeper look at the implementation whether there is somer point where the process can be aborted.


#5

The URL::createInputStream() convenience method returns a WebInputStream object, which has a cancel() method.

it seems the usual usage format is:

juce::ScopedPointer<juce::InputStream> stream(
    serverRequestURL.createInputStream(true, nullptr, nullptr, juce::String(), 10000, nullptr, &statusCode)
);
stream->readEntireStreamAsString();

It’s just a guess, but instead of creating an InputStream, you could create a WebInputStream instead, check if you need to cancel it before creating the InputStream and calling readEntireStreamAsString().