Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1039837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:08:59+00:00 2026-05-16T15:08:59+00:00

I download a file by e.g. 5 threads. When one of the threads completes

  • 0

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;
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T15:08:59+00:00Added an answer on May 16, 2026 at 3:08 pm

    In general, your approach is not correct because you shouldn’t ever end your thread by calling Abort(). This ends up raising an exception. The approach I would try is to just allow the thread to return instead of aborting. You could do this by passing the thread info about when to return. Another way (which is probably better) is to use an AutoResetEvent, and Set() it when you want each thread to end. The code inside of the thread would have something like:

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

    It looks like you want these 5 threads to share information about the download status, i.e. how many bytes have been downloaded by each thread. If you want to have real-time updating (which I assume), you could pass a reference to the count into each thread, and ensure that each thread uses a lock to protect access to this variable. Or you can have it only update the byte count every 1K or more, so that you’re not locking and unlocking a bunch.

    If you have a worker thread that manages these multiple downloading threads, then you can start each thread and then use EndInvoke from the “main” worker thread (i.e. not main thread itself or your GUI will block) to make sure each thread has ended properly. Or you can use an AsyncCallback, which gets called when each thread is done. Within the AsyncCallback, you can call EndInvoke.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to download a file from internet with progress bar using threads in Delphi
How do I download a file from the internet in a Flex based AIR
Is it possible to download a file from a website in Windows Application form
I'm using urllib.urlretrieve to download a file, and implementing a download progress bar using
I have to login into a https web page and download a file using
I have just coded up a web based flv file download utility. When I
I'm trying to use SQL Server 2000 DTS Package to download a file from
We have a static method in a utility class that will download a file
I've done numerous searches and I realize that I can just download this file
If I download a .gem file to a folder in my computer, can I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.