abcd = new Thread(() =>
{
bool done = false;
try
{
long fileSize = Download.GetFileSize("blah");
HttpWebRequest request = Download.CreateDownloadRequest("blah");
request.Timeout = 3500;
request.AddRange(0, (int)fileSize - 1);
request.Referer = Download.GetReffer(Server.SinaBlog);
byte[] buffer = new byte[8192];
byte[] png = new byte[fileSize];
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (Stream stream = request.GetResponse().GetResponseStream())
{
streamDDDDD = stream;
int receivedBytes = 0;
for (int readBytes = stream.Read(buffer, 0, buffer.Length); readBytes > 0;)
{
Array.Copy(buffer, 0, png, receivedBytes, readBytes);
receivedBytes += readBytes;
lock (locker)
{
AcceptedBytes[index] = receivedBytes;
FinishedBytes += readBytes;
}
readBytes = stream.Read(buffer, 0, buffer.Length);
}
}
}
//do some IO to write this buffer into my hardisk. This Buffer is just part of a very big file!
done = true;
request.Abort();
}
catch(Exception ae)
{
MessageBox.Show(ae.Message);
}
if(done)
MessageBox.Show("done");
}){ IsBackground = true };
abcd.Start();
In this case, for instance, assume I start it with 10KB/S net-speed and I pull the internet cable, this thread will just freeze at all. Even I use abcd.Abort(), the messageBox will show after many seconds later. Besides, Abort() is disappreciated.
However, If I use streamDDDDD.Close(), it will throw an exception, something like WSACancelBlockingCall exception.
My purpose is, when I need to "abort" this thread,
- I want it stop as soon as possible. Before, for a very very long time, I thought Abort() is unsafe but very effective, so I insist to use it, regardless of Microsoft's official and other's defaming Abort(). But today I finally discovered such a case that Abort() not work as immediately as usual, make me so excited and give me no reason to use Abort() anymore. However, streamDDDDD.Close() looks great, but it throws exception not always the same. sometimes it will throws like connection unexpected closed, sometimes WSACancelBlockingCall, which is so strange , such an undefined behavior make me dare not use it.
It just looks strange, but I can't find any official document about how to shutdown a blocked stream, or tenicially, a stream we don't know if it is blocked or not...
2.I must make sure I can 100% catch the exception. as I put the locker block in the middle, when this thread is manually aborted(), I must do something to retreat,like
AcceptedBytes[index] = 0;
FinishedBytes -= readBytes;
besides, there is a case that, I don't know whether the thread's progress, like it is in the download phase of the IO phase...
So, can any help would be appreciated and Thank you.