Finding it impossible to cancel a web download

M

Malc

Hi,

I'm using the HttpWebRequest class to download a file from the Internet.
I'd would like to provide support for cancelling downloads. The moment I
get the repsponse stream it appears to start downloading into a hidden
buffer. So if I cancel reading from the response stream before Ive read
all the data from it, and then attempt to close the response stream it
hangs on the close call until it has downloaded all of the data into its
internal buffer. I've tried sync and async techniques to no avail. Very
annoying! Anyone know the workaround? Tia!

Here is my async attempt - you can see I break out of the read cycle early
once 100000 bytes have been read.

private static void ReadCallBack(IAsyncResult asyncResult)
{
// Get the RequestState object from AsyncResult.
RequestState rs = (RequestState)asyncResult.AsyncState;

// Retrieve the ResponseStream that was set in RespCallback.
Stream responseStream = rs.ResponseStream;

// Read rs.BufferRead to verify that it contains data.
int read = responseStream.EndRead( asyncResult );
if (read > 0 && !rs.abortRequest)
{
Array.Copy(rs.BufferRead,0,rs.RequestData,rs.RequestIndex,read);
rs.RequestIndex+=read;

if(rs.RequestIndex>100000)
{
rs.StartPlaying();
rs.abortRequest=true;

}
// Continue reading data until
// responseStream.EndRead returns –1.
IAsyncResult ar = responseStream.BeginRead(
rs.BufferRead, 0, BUFFER_SIZE,
new AsyncCallback(ReadCallBack), rs);
}
else
{
if(rs.RequestData.Length>0)
{
// Display data to the console.
//string strContent;
//strContent = rs.RequestData.ToString();

}
// Close down the response stream.

responseStream.Close(); // HANGS HERE UNTIL ALL DATA HAS BEEN
DOWNLOADED TO INTERNAL RESPONSE STREAM BUFFER
rs.Finished();

// Set the ManualResetEvent so the main thread can exit.
// allDone.Set();
}
return;
}

I was using the info from the post below to add in the abortRequest
parameter into my AsyncState, but due to the lower level buffering on CF
it has no effect.
http://groups-beta.google.com/group..._doneTitle=Back+to+Search&&d#7666aaa0bb8a84af

I notice the HttpRequest has a AllowWriteStreamBuffering flag, perhaps I
am looking for something like AllowReadStreamBuffering?
 
M

Malc

I should perhaps add that I've monitored the outgoing data on the PC that
the PDA is downloading from, and when it has broken out of its read loop
and is stuck on the responsestream.close() call I can see that the PC is
still sending at full speed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top