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

  • SEARCH
  • Home
  • 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 6595581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:57:30+00:00 2026-05-25T17:57:30+00:00

I’m new in c# and i’m still working way through and learning it all.

  • 0

I’m new in c# and i’m still working way through and learning it all.

I have made this code for a progress bar when downloading a file from ftp, and it actually working just fine. But progress value is all wrong. It looks like it is byte value somehow. But the weird thing is when I print the value to the screen, then it prints the correct value.

private void frm_movie_db_Load(object sender, EventArgs e)
{
    if (!File.Exists("movies.list.gz"))
    {
        bg_worker.RunWorkerAsync();
    }
}

private void bg_worker_DoWork(object sender, DoWorkEventArgs e)
{
    string strDownloadFrom = "ftp://ftp.sunet.se/pub/tv+movies/imdb/movies.list.gz";
    string strDownloadTo = "movies.list.gz";

    try
    {
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(strDownloadFrom);

        request.Method = WebRequestMethods.Ftp.GetFileSize;
        request.Credentials = new NetworkCredential("anonymous", "");
        request.UsePassive = true;
        request.UseBinary = true;
        request.KeepAlive = true;

        Int64 intFileSize = request.GetResponse().ContentLength;
        Int64 intRunningByteTotal = 0;

        request = (FtpWebRequest)FtpWebRequest.Create(strDownloadFrom);
        request.Method = WebRequestMethods.Ftp.DownloadFile;
        request.Credentials = new NetworkCredential("anonymous", "");
        request.UsePassive = true;
        request.UseBinary = true;
        request.KeepAlive = false;

        FtpWebResponse response = (FtpWebResponse)request.GetResponse();
        Stream reader = response.GetResponseStream();

        Stream writer = new FileStream(strDownloadTo, FileMode.Create, FileAccess.Write, FileShare.None);
        byte[] byteBuffer = new byte[1024];

        int intByteSize = 0;
        int intProgressPct = 0;

        while ((intByteSize = reader.Read(byteBuffer, 0, byteBuffer.Length)) > 0)
        {
            if (intByteSize == 0)
            {
                intProgressPct = 100;
                bg_worker.ReportProgress(intProgressPct);
            }
            else
            {
                writer.Write(byteBuffer, 0, intByteSize);

                if (intByteSize + intRunningByteTotal <= intFileSize)
                {
                    intRunningByteTotal += intByteSize;
                    double dIndex = intRunningByteTotal;
                    double dTotal = byteBuffer.Length;
                    double dProgressPct = (double)(dIndex / dTotal);
                    intProgressPct = (int)dProgressPct;
                    bg_worker.ReportProgress(intProgressPct);
                }
            }
        }

        //reader.Close();
        //mem_stream.Close();
        //response.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

private void bg_worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    //pb_download_files.Value = e.ProgressPercentage;
    lbl_progress_pct.Text = e.ProgressPercentage.ToString() + "%";
}

private void bg_worker_RunWorkerComplete(object sender, RunWorkerCompletedEventArgs e)
{
    pnlProgress.Visible = false;
}

I hope someone can help me with this, as i’ve done everything so far to fix the problem myself.

Best regards
Jesper

  • 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-25T17:57:30+00:00Added an answer on May 25, 2026 at 5:57 pm

    You’re completely mistaken on percentage progress calculation: perc must be
    100 x current / total, so you’re using wrong values.

    Try with this:

    double dProgressPct = 100.0 * intRunningByteTotal / intFileSize;
    bg_worker.ReportProgress((int)ProgressPct);
    

    Read Microsoft documentation:

    public void ReportProgress(int percentProgress)
    

    where percentageProgress is the percentage, from 0 to 100, of the background operation that is complete.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't

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.