C#进度条在下载时不会更新

时间:2018-05-21 16:48:06

标签: c# .net download ftp progress-bar

我正在使用BackgroundWorker从ftp服务器下载文件,但只有在文件下载完成后才会更新进度。

进度条保持为0,在下载期间冻结,并设置为100。

这是我的代码:

    private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) {
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(activeDownload.ftpPath);
        request.Credentials = new NetworkCredential(ftpUser, ftpPass);
        request.Method = WebRequestMethods.Ftp.DownloadFile;

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

        using (FileStream writer = new FileStream(activeDownload.localPath, FileMode.Create)) {

            long length = response.ContentLength;
            int bufferSize = 2048;
            int readCount;
            byte[] buffer = new byte[2048];
            long bytesread = 0;
            backgroundWorker1.ReportProgress(0);

            readCount = responseStream.Read(buffer, 0, bufferSize);
            while (readCount > 0) {
                bytesread += readCount;
                writer.Write(buffer, 0, readCount);
                backgroundWorker1.ReportProgress((int)(bytesread / activeDownload.fileSize) * 100);
                readCount = responseStream.Read(buffer, 0, bufferSize);
            }
        }

        response.Close();
        responseStream.Close();
    }

    private void backgroundWorker1_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e) {
        progressBar1.Value = e.ProgressPercentage;
    }

我做错了什么?任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

查看以下链接是否有帮助:

From StackMSDN

以下是我在其中一个项目中实施的示例代码:

public ProcessFilesClass()
    {
        InitializeComponent();
        backgroundWorker1= new BackgroundWorker();
        backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
        backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
        backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
    }

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = sender as BackgroundWorker;
        var backgroundWorker = sender as BackgroundWorker;
        worker.WorkerReportsProgress = true;
        WorkerProcessMethod();
    }

private void WorkerProcessMethod()
    {
            //Process items in list box
            int itemsToProcess = someListBox.Items.Count;
            for (int i = 0; i < itemsToProcess; i++)
                {
                    int findPercentage = ((i + 1) * 100) / someListBox.Items.Count;
                     if (backgroundWorker1 != null) { backgroundWorker1.ReportProgress(findPercentage, null); }
                }
    }

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
           progressBar1.Value = e.ProgressPercentage;

    }

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
          //DisableProgressBarStatus
           this.Close();            
    }

答案 1 :(得分:0)

好的,事实证明我做的一切都很好,但我正在解析错误的百分比。除以两个long(bytesread和activeDownload.fileSize)返回0.因此,进度停留在0%。

这是正确的解析:

backgroundWorker1.ReportProgress(Convert.ToInt32((((double)bytesread / activeDownload.fileSize) * 100)));