开发者

Progress getting incorrect value from backgroundworker

开发者 https://www.devze.com 2023-04-06 12:08 出处:网络
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

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开发者_如何学JAVA = 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


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.


double dTotal = byteBuffer.Length;

Does not assign the total bytes to dTotal. byteBuffer is a buffer with a constant size of 1024 bytes. Try something like

double dTotal = reader.Length;

to retrieve the length in bytes of the stream.


you have:

double dTotal = byteBuffer.Length;
double dProgressPct = (double)(dIndex / dTotal);

I think you want:

double dProgressPct = (double)(dIndex / intFileSize );
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号