I download a file by e.g. 5 threads. When one of the threads completes downloading the file part - it is aborted, BUT all of the rest threads has the ThreadState = WaitSleepJoin and obviously stops downloading. How to resolve that ?
while ((bytesSize = responseStream.Read(Buffer, 0, Buffer.Length)) > 0)
{
lock(fileStream)
{
fileStream.Write(Buffer, 0, bytesSize);
if (DownloadedBytesCount >= EndPosition - StartPosition)
{
downThread.Abort();
break;
}
else DownloadedBytesCount += bytesSize;
}
}
I suppose that the fileStream is still blocked after downThread.Abort(). I thought that the break unlocks the file stream but it's not. So how to unlock that file ?
Here You have some more info:
I have a class "ThreadFileManager":
public class ThreadFileManager
{
public ThreadContent[] threads;
protected static FileStream fileStream { get; set; }
public ThreadFileManager(string fileUrl, string LocalPath, int ThreadCount, int bufferLength, ProgressBar progressBarReference)
{
if (File.Exists(LocalPath))
fileStream = new FileStream(LocalPath, FileMode.Append, FileAccess.Write);
else fileStream = new FileStream(LocalPath, FileMode.Create, FileAccess.Write);
CreateThreads(fileUrl, ThreadCount, bufferLength); //create threads and start downloading
}
private void CreateThreads(string fileUrl, int ThreadCount, int bufferLength)
{
webRequest = (HttpWebRequest)WebRequest.Create(fileUrl);
webRequest.Credentials = CredentialCache.DefaultCredentials;
webResponse = (HttpWebResponse)webRequest.GetResponse();
responseStream = webResponse.GetResponseStream();
//calculate the total bytes count to download per one thread
long part = webResponse.ContentLength / ThreadCount;
fileLength = webResponse.ContentLength;
this.threads = new ThreadContent[ThreadCount];
ThreadContent thr_cn = new ThreadContent(bufferLength, fileUrl);
thr_cn.StartPosition = 0;
thr_cn.EndPosition = part;
threads[0] = thr_cn;
for (int i = 1; i < ThreadCount; i++)
{
thr_cn = new ThreadContent(bufferLength, fileUrl);
thr_cn.StartPosition = (i * part) + 1;
thr_cn.EndPosition = (i + 1) * part;
this.threads[i] = thr_cn;
}
}
}
public class ThreadContent : ThreadFileManager
{
public long StartPosition { get; set; } //the Begining position of the downloading file
public long EndPosition { get; set; } //the End position of the downloading file
public byte[] Buffer { get; set; }
HttpWebRequest webRequest { get; set; }
HttpWebResponse webResponse { get; set; }
long BufferLength { get; set; }
long DownloadedBytesCount { get; set; }
Thread downThread;
string FileURL { get; set; }
public ThreadContent(int bufferLength, string url)
{
Buffer = new byte[bufferLength];
downThread = new Thread(new ThreadStart(Download));
FileURL = url;
}
public void Download()
{
int bytesSize = 0;
webRequest = (HttpWebRequest)WebRequest.Create(FileURL);
webRequest.Credentials = CredentialCache.DefaultCredentials;
webResponse = (HttpWebResponse)webRequest.GetResponse();
responseStream = webResponse.GetResponseStream();
webRequest.AddRange(StartPosition, EndPosition);
while ((bytesSize = responseStream.Read(Buffer, 0, Buffer.Length)) > 0)
{
lock (fileStream)
{
fileStream.Write(Buffer, 0, bytesSize);
base.UpdateProgress(bytesSize);
}
if (DownloadedBytesCount >= EndPosition - StartPosition)
{
downThread.Abort();
break;
}
else DownloadedBytesCount += bytesSize;
}
}