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 531393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:17:48+00:00 2026-05-13T09:17:48+00:00

Edit: This is done in the Compact Framework, I don’t have access to WebClient

  • 0

Edit: This is done in the Compact Framework, I don’t have access to WebClient therefore it has to be done with HttpWebRequests.

I am creating a download manager application that will be able to have concurrent downloads (more than one download at once) and the ability to report the percentage completed and resume the downloads.

This means that I am downloading some bytes into a buffer and then writing the buffer to disk. I just wanted to check what recommended algorithm/procedure is for this.

This is what I have thus far for the main download method:

    private void StartDownload()
    {
        HttpWebRequest webReq = null;
        HttpWebResponse webRes = null;
        Stream fileBytes = null;
        FileStream saveStream = null;

        try
        {
            webReq = (HttpWebRequest)HttpWebRequest.Create(_url);
            webReq.Headers.Add(HttpRequestHeader.Cookie, "somedata");
            webRes = (HttpWebResponse)webReq.GetResponse();

            byte[] buffer = new byte[4096];
            long bytesRead = 0;
            long contentLength = webRes.ContentLength;

            if (File.Exists(_filePath))
            {
                bytesRead = new FileInfo(_filePath).Length;
            }

            fileBytes = webRes.GetResponseStream();
            fileBytes.Seek(bytesRead, SeekOrigin.Begin);

            saveStream = new FileStream(_filePath, FileMode.Append, FileAccess.Write);

            while (bytesRead < contentLength)
            {
                int read = fileBytes.Read(buffer, 0, 4096);
                saveStream.Write(buffer, 0, read);
                bytesRead += read;
            }
            //set download status to complete
            //_parent
        }
        catch
        {
            if (Thread.CurrentThread.ThreadState != ThreadState.AbortRequested)
            {
               //Set status to error.
            }
        }
        finally
        {
            saveStream.Close();
            fileBytes.Close();
            webRes.Close();
            saveStream.Dispose();
            fileBytes.Dispose();
            saveStream = null;
            fileBytes = null;
            webRes = null;
            webReq = null;
        }
    }

Should I be downloading a larger buffer? Should I be writing the buffer to file so often (every 4KB?) Should there be some thread sleeping in there to ensure not all the CPU is used? I think reporting the progress change every 4KB is stupid so I was planning to do it every 64KB downloaded.

Looking for some general tips or anything that is wrong with my code so far.

  • 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-13T09:17:48+00:00Added an answer on May 13, 2026 at 9:17 am

    First thing, I would get rid of the finally clause and change the code to use “USING” clauses.

    Anything that implements IDisposable should be programmed that way to make sure garbage collection occurs correctly and when it is supposed to.

    For example:

    using (HttpWebRequest webReq = (HttpWebRequest)HttpWebRequest.Create(_url)) {
        /* more code here... */
    }
    

    Second, I wouldn’t instantiate my variables at the head with null values (ala Pascal style). See above example.

    Third, the download should be in it’s own thread which sync’s with a call back function in the main thread to report status. Put the sync call in the middle of your while loop.

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

Sidebar

Ask A Question

Stats

  • Questions 275k
  • Answers 275k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Apparently this is by design: This is by design. The… May 13, 2026 at 2:36 pm
  • Editorial Team
    Editorial Team added an answer There are a few ways you can do this. The… May 13, 2026 at 2:36 pm
  • Editorial Team
    Editorial Team added an answer If an IFRAME is loaded from a different domain than… May 13, 2026 at 2:36 pm

Related Questions

Iv been encountering problems with disk space when deploying my app to a pocket
I have an FLA done in Flash 10, and some AS3 code that manipulates
Is there any way to determine a removable drive speed in Windows without actually
I'm making a WinForms app in C#. When I go to create my database
Why in the ADVANCE section when I 'configure data source' is the 'GENERATE INSERT

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.