在后台工作线程中使用进度条显示下载进度

本文关键字:显示 下载 后台 工作 线程 | 更新日期: 2023-09-27 18:10:56

我正在编写一个每次下载一个文件的应用程序,到目前为止一切都很好,但是当我试图让进度条显示下载进度时,我遇到了一些问题。我以前做过,但这是我第一次在后台工作线程上做所有这些。我使用DownloadFile而不是DownloadFileAsync的原因是因为我需要它一次下载一个文件,我发现这样做更容易一些。

是代码的一部分:

    private void button1_Click(object sender, EventArgs e)
    {
        BackgroundWorker bw = new BackgroundWorker();
        bw.DoWork += bw_DoWork;
        bw.ProgressChanged += bw_ProgressChanged;
        bw.RunWorkerCompleted += bw_RunWorkerCompleted;
        bw.WorkerReportsProgress = true;
        if (!bw.IsBusy)
        {
            bw.RunWorkerAsync();
        }

    }
    void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        MessageBox.Show("Patching Completed.");
    }
    void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        barPatch.Value = e.ProgressPercentage;
    }
    void bw_DoWork(object sender, DoWorkEventArgs e)
    {
        doPatch();
    }
    private void doPatch()
    {
        for (int C = int.Parse(File.ReadAllText("Client.txt")); C < S; C++)
        {
            this.lblC.InvokeEx(lbl => lblC.Text = Convert.ToString(C + 1));
            wc.DownloadFile(String.Format("http://localhost:1234/Launcher/Patches/{0}.zip", C + 1), String.Format("{0}.zip", C + 1));
            File.WriteAllText("Client.txt", Convert.ToString(C + 1));
        }
    }
    void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {

    }

任何帮助都是感激的!由于

在后台工作线程中使用进度条显示下载进度

可以用wc.DownloadFileAsync代替wc.DownloadFile

wc_DownloadProgressChanged中使用

bw.ReportProgress(e.BytesReceived / e.TotalBytesToReceive * 100);

你可以访问

WebClient。DownloadProgressChanged事件

BackgroundWorker。ReportProgress Method (Int32)

获取更多帮助

这是一个非常肮脏的技巧,但是如果你这样做,你可以得到任何文件大小更改的进度:

        class InfoForDownload
        {
              public string FileName {get; set;}
              public int FileSize {get; set;}
        }
        int fileSizeFromCheck;
        InfoForDownload info = new InfoForDownload() { FileName = "C:'Users'TheUser'OneDrive'Documents'etc'etc'backup.sql", FileSize = THEFILESIZEOFTHEFILEONEXTERNAL };
        BackgroundWorker checkDownload = new BackgroundWorker();
        checkDownload.WorkerReportsProgress = true;
        checkDownload.ProgressChanged += checkDownload_ProgressChanged;
        checkDownload.DoWork += checkDownload_DoWork;
        checkDownload.RunWorkerAsync(info);
        void downloadWorker_DoWork(object sender, DoWorkEventArgs e)
        {
              BackgroundWorker checkDownload = (sender as BackgroundWorker);
              if (e.Argument != null)
              {
                    InfoForDownload info = (sender as InfoForDownload);
                    ProcessStartInfo cmdStartInfo = new ProcessStartInfo();
                    cmdStartInfo.FileName = @"C:'Windows'System32'cmd.exe";
                    cmdStartInfo.RedirectStandardOutput = true;
                    cmdStartInfo.RedirectStandardError = true;
                    cmdStartInfo.RedirectStandardInput = true;
                    cmdStartInfo.UseShellExecute = false;
                    cmdStartInfo.CreateNoWindow = true;
                    while(info.FileSize > fileSizeFromCheck){
                          Process cmdProcess = new Process();
                          cmdProcess.StartInfo = cmdStartInfo;
                          cmdProcess.ErrorDataReceived += cmd_Error;
                          cmdProcess.OutputDataReceived += cmd_DataReceived;
                          cmdProcess.EnableRaisingEvents = true;
                          cmdProcess.Start();
                          cmdProcess.BeginOutputReadLine();
                          cmdProcess.BeginErrorReadLine();
                          cmdProcess.StandardInput.WriteLine("for %I in (info.FileName) do @echo %~zI");
                          cmdProcess.StandardInput.WriteLine("exit");
                          cmdProcess.WaitForExit();
                          Thread.Sleep(200);
                          Console.WriteLine("The size of the file is " + fileSizeFromCheck + " " + (info.FileSize / fileSizeFromCheck  * 100) + "%");
                          checkDownload.ReportProgress(info.FileSize / fileSizeFromCheck  * 100);
                    }
              }
        }
        void checkDownload_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
              progressBar.Value = e.ProgressPercentage;
        }
        static void cmd_DataReceived(object sender, DataReceivedEventArgs e)
        {
              Console.WriteLine("Output from other process");
              Console.WriteLine(e.Data);
              Int32.TryParse((e.Data / 1024) / 1024, out fileSizeFromCheck);
        }

进行编辑以适应您的使用和名为progressbar的进度条。瞧。脏到不能再脏