J
Jeff Robichaud
Hi all,
I have some code running on the Pocket PC (w/CF v2.0) that grabs a file
(about 20 MB) from the server using HttpWebRequest. I want to give to user
the possibility to cancel the download (the download method is running in
another thread which is aborted when the user cancels).
From what I understand, to close everything gracefully requires calling
Abort() on the HttpWebRequest object and/or Close() on the WebResponse
object. Failure to do so will interfere in the subsequent attempts to
download the file.
But here goes, calling either of the methods seems to hang the device, but
in fact I think there is some cleanup going on in the backgroup. BUT, from a
user point of view, it changes nothing. Is there a bug in the CF because the
same code works A1 on the desktop.
Has anybody been able to work around this, or am I simply missing something?
Thanks.
Here's a lean version of the code:
private void DownloadFile()
{
HttpWebRequest request = null;
WebResponse response = null;
Stream stream = null;
FileStream fs = null;
try
{
request = (HttpWebRequest)WebRequest.Create(_remoteFileUrl);
request.KeepAlive = false;
response = request.GetResponse();
stream = response.GetResponseStream();
string localFilePath = Path.GetTempPath() +
Path.GetFileName(_remoteFileUrl);
fs = new FileStream(localFilePath, FileMode.Create);
int total = 0;
byte[] buffer = new byte[0x1000];
while (true)
{
int currentRead = stream.Read(buffer, 0, buffer.Length);
total += currentRead;
fs.Write(buffer, 0, currentRead);
if (currentRead == 0) { break; }
}
}
finally
{
fs.Close();
response.Close();
request.Abort();
}
}
I have some code running on the Pocket PC (w/CF v2.0) that grabs a file
(about 20 MB) from the server using HttpWebRequest. I want to give to user
the possibility to cancel the download (the download method is running in
another thread which is aborted when the user cancels).
From what I understand, to close everything gracefully requires calling
Abort() on the HttpWebRequest object and/or Close() on the WebResponse
object. Failure to do so will interfere in the subsequent attempts to
download the file.
But here goes, calling either of the methods seems to hang the device, but
in fact I think there is some cleanup going on in the backgroup. BUT, from a
user point of view, it changes nothing. Is there a bug in the CF because the
same code works A1 on the desktop.
Has anybody been able to work around this, or am I simply missing something?
Thanks.
Here's a lean version of the code:
private void DownloadFile()
{
HttpWebRequest request = null;
WebResponse response = null;
Stream stream = null;
FileStream fs = null;
try
{
request = (HttpWebRequest)WebRequest.Create(_remoteFileUrl);
request.KeepAlive = false;
response = request.GetResponse();
stream = response.GetResponseStream();
string localFilePath = Path.GetTempPath() +
Path.GetFileName(_remoteFileUrl);
fs = new FileStream(localFilePath, FileMode.Create);
int total = 0;
byte[] buffer = new byte[0x1000];
while (true)
{
int currentRead = stream.Read(buffer, 0, buffer.Length);
total += currentRead;
fs.Write(buffer, 0, currentRead);
if (currentRead == 0) { break; }
}
}
finally
{
fs.Close();
response.Close();
request.Abort();
}
}