Read download data using Backgroundworker Progresschanged

Asked

Viewed 57 times

0

td well? I am very new in programming, I took a project of a Auncher with autoupdate already ready and just edited some things. It basically downloads a game’s updates.

I’ve handled other projects before that used only Webclient to download, but this Leacher uses Backgroundworker, and so far no problem. Only this Backgroundworker uses System.ComponentModel.Progresschangedeventhandler and it does not have the bytesReceived and totalBytesToReceive options as it has in Webclient. So I can’t get the download data to show how much was downloaded and how much is missing.

Code using Webclient.Downloadprogresschangedeventargs:

private void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        progressBar1.Value = e.ProgressPercentage;
        downloadLbl.Text = "Downloading Updates";
        label1.Text = e.ProgressPercentage + "%";
        double bytesIn = double.Parse(e.BytesReceived.ToString());
        double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
        double percentage = bytesIn / totalBytes * 100;
        label2.Text = "Downloaded " + e.BytesReceived + " of " + e.TotalBytesToReceive;

    }

If I put this above code no error appears, but when you run the program and download, the progressbar and the labels do not work because you are using Backgroundworker to download the file.

Code using Backgroundworkder Componentmodel.Progresschangedeventargs:

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {

        progressBar1.Value = e.ProgressPercentage;
        downloadLbl.Text = "Downloading Updates";
        label1.Text = e.ProgressPercentage + "%";
        double bytesIn = double.Parse(e.BytesReceived.ToString());
        double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
        double percentage = bytesIn / totalBytes * 100;
        label2.Text = "Downloaded " + e.BytesReceived + " of " + e.TotalBytesToReceive;

    }

Using this code the progressibar works and the progress in % of the label1 too, but the label2 with the download data and how much to finish do not work and Totalbytestoreceive and Bytesreceived are underlined in red with an error saying that "Progresschangedeventargs" does not contain a definition for Totalbytestoreceive/Bytesreceived.

Does anyone know any solution or gambiarra that I can do to solve this problem? Thanks!!

  • Why not use a Webclient.Downloadasync()?

1 answer

0

Good morning, Progresschangedeventargs do not actually contain these properties, as you can see here: https://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.progresschanged(v=vs.110). aspx

Try something similar to this code by changing the necessary:

public partial class Update : Form
{
    public Update()
    {
        InitializeComponent();
    }

    private void Update_Load(object sender, EventArgs e)
    {
        WebClient client = new WebClient();
        string desktop = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
        client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
        client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
   client.DownloadFileAsync(new Uri("http://download.yourfile.zip"), desktop + "teste.zip");
    }

    void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        progressBar1.Maximum = (int)e.TotalBytesToReceive / 100;
        progressBar1.Value = (int)e.BytesReceived / 100;
    }

    void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
    {
        label2.Text = "Download Completed";
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.