Read download data using ProgressChanged BackgroundWorker

0

Okay? I'm very new to programming, I got a project from a launcher with autoupdate ready and just edited a few things. It basically downloads the updates of a game.

I already moved with other projects before they used only the WebClient to download, however this launcher uses the BackgroundWorker, and until then neither problem. Only this BackgroundWorker uses the System.ComponentModel.ProgressChangedEventHandler and in it does not have the options of bytesReceived and totalBytesToReceive as it has in the WebClient. So I can not get the download data to show how much has been 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 code above no error appears, however when you run the program and download, progressbar and label do not work because you are using BackgroundWorker to download the file.

Code using the 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 progressbar works and progress on% label1 as well, but the label2 with the download data and what's left to finish does not work and TotalBytesToReceive and BytesReceived are underlined in red with an error saying "ProgressChangedEventArgs" does not contain a setting for TotalBytesToReceive / BytesReceived.

Does anyone know of any solution or game that I can do to solve this problem? Thank you !!

    
asked by anonymous 19.02.2018 / 03:17

1 answer

0

Good morning, ProgressChangedEventArgs does not effectively contain these properties, as you can see here: link

Try something similar to this code by changing what you need:

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";
    }
}
    
19.02.2018 / 11:37