Close reads the rest of the stream? (HTTP) Another really bad CF bug?

H

Hilton

Hi,

I have a while (true) look reading bytes from an
httpresponse.GetResponseStream() and writing them to a file. If the user
presses a Cancel button, I do a "break", the loop exits, then the app gets
stuck on "responsestream.Close ();" which reads the entire rest of the
stream before closing! This part has nothing to do with the for-loop, the
app just sits in the Close(). How can I get the app to just be done with
the stream, close it and move on without reading in the megabytes that might
follow? Why is Close() reading the rest of the stream?

Any suggestions are appreciated? I have tried various things, most cause
exceptions and/or stock future downloads from working (not sure why about
this one either). FWIW: The code runs perfectly on the desktop; i.e. the
Close() does not download the rest of the stream. Is this (another) really
bad CF bug or am I doing something very wrong? I hope I'm wrong.

Code is below...

Thanks,

Hilton
-----------
using (FileStream fs = new FileStream (tmpFilename,
FileMode.Create, FileAccess.Write))
{
bool checkedEqual = false;

// Data buffer
byte[] buffer = new byte [BUFFER_SIZE];

// Copy all the bytes from the HTTP stream to the
file
do
{
int bytesRead = responsestream.Read (buffer, 0,
BUFFER_SIZE);

// End of stream?
if (bytesRead == 0)
{
break;
}

// If not, write the bytes we have
fs.Write (buffer, 0, bytesRead);

bytesDownloaded += bytesRead;

// Fire off the progress events.
if (peh != null)
{
ProgressEventArgs pea = new
ProgressEventArgs (dataSize, bytesDownloaded);

peh (null, pea);

if (pea.Cancel)
{
break;
}
}
} while (true);
}

// This Close() reads the rest of the
stream!!!!!!!!!!!!!!!!!!!!!!!!!!!
responsestream.Close ();
 
T

Tomppa

In the checked change event of your UI thread set a variable in a singleton
class that your loop checks.
 
H

Hilton

The code doesn't hang in my loop. It hangs after the break; i.e. on the
Close() line. I put a MessageBox just before the Close() line and one just
after it. The loop exists, stops on the MessageBox, as I click OK off it
goes downloading the rest of the file!!! Why?!?!? Then after a long wait,
it hits the second MessageBox; i.e.:

MessageBox.Show ("1");
responsestream.Close ();
MessageBox.Show ("2");

FYI: I know it is downloading and not just pausing because:
1. I see the activity on my modem and WAP
2. The delay to get to the second MessageBox call is directly proportional
to the size of the file.

Hilton


Tomppa said:
In the checked change event of your UI thread set a variable in a
singleton class that your loop checks.

Hilton said:
Hi,

I have a while (true) look reading bytes from an
httpresponse.GetResponseStream() and writing them to a file. If the user
presses a Cancel button, I do a "break", the loop exits, then the app
gets stuck on "responsestream.Close ();" which reads the entire rest of
the stream before closing! This part has nothing to do with the
for-loop, the app just sits in the Close(). How can I get the app to
just be done with the stream, close it and move on without reading in the
megabytes that might follow? Why is Close() reading the rest of the
stream?

Any suggestions are appreciated? I have tried various things, most cause
exceptions and/or stock future downloads from working (not sure why about
this one either). FWIW: The code runs perfectly on the desktop; i.e. the
Close() does not download the rest of the stream. Is this (another)
really bad CF bug or am I doing something very wrong? I hope I'm wrong.

Code is below...

Thanks,

Hilton
-----------
using (FileStream fs = new FileStream (tmpFilename,
FileMode.Create, FileAccess.Write))
{
bool checkedEqual = false;

// Data buffer
byte[] buffer = new byte [BUFFER_SIZE];

// Copy all the bytes from the HTTP stream to the
file
do
{
int bytesRead = responsestream.Read (buffer,
0, BUFFER_SIZE);

// End of stream?
if (bytesRead == 0)
{
break;
}

// If not, write the bytes we have
fs.Write (buffer, 0, bytesRead);

bytesDownloaded += bytesRead;

// Fire off the progress events.
if (peh != null)
{
ProgressEventArgs pea = new
ProgressEventArgs (dataSize, bytesDownloaded);

peh (null, pea);

if (pea.Cancel)
{
break;
}
}
} while (true);
}

// This Close() reads the rest of the
stream!!!!!!!!!!!!!!!!!!!!!!!!!!!
responsestream.Close ();
 

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