Unlock FIleStream when one of the download threads terminates

I upload a file, for example. 5 threads. When one of the threads finishes loading part of the file - it is interrupted, BUT all other threads have ThreadState = WaitSleepJoin and, obviously, stop loading. How to solve this?

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 believe fileStream is still locked after downThread.Abort (). I thought breaking would unlock the file stream, but it is not. So how to unlock this file?

Here you have additional information:

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;
        }
    }
+3
source share
2 answers

, , , Abort(). . , , . , , . (, ) - AutoResetEvent Set(), , . - :

AutoResetEvent e = new AutoResetEvent( false);
while( !e.WaitOne( 0)) {
  // your code here
}

, , 5 , . ( ), , . 1K , .

, , , EndInvoke "" (.. ), , . AsyncCallback, . AsyncCallback EndInvoke.

+3

,

Thread.Abort() , . , break, , Thread.

, , :

  • Thread.Start(). , ? , , WaitSleepJoin.

  • . , , .

  • ThreadContent ThreadFileManager. (), : - Filemanager ().

  • downThread , Download() Thread.CurrentThread.

  • ThreadPool .


, .

MSDN,

Abort ,

, lock , .

fileStream , , Position .

+3

Source: https://habr.com/ru/post/1763313/


All Articles