Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The app downloading multiple files asynchronously, so when i enter 10 links into listbox 10 files start to download. I wast to create progress bars dynamically for every file and report progress. This is what im using to report progress
C#
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(worker_ProgressChanged);
but its not working.
When i do
C#
worker.ReportProgress(10);

it works but im can only report an integer and not client.DownloadProgressChanged.
My question is how do i attach client.DownloadProgressChanged to my worker_ProgressChanged class.

What I have tried:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{


List<string> myList = (List<string>)e.Argument;

foreach(string yo in myList)
{
ProgressBar bar = new ProgressBar();
position += 30;
bar.Width = 300;

bar.Location = new Point(300, position);

Invoke(new ToDoDelegate(() => bar.Location = new Point(150, position)));
Invoke(new ToDoDelegate(() => this.Controls.Add(bar)));
Invoke(new ToDoDelegate(() => bar.Visible = true));


WebClient client = new WebClient();

string FileName = yo.Substring(yo.LastIndexOf("/") + 1, (yo.Length - yo.LastIndexOf("/") - 1));
client.DownloadFileAsync(new Uri(yo), "C:\\Test4\\" + FileName);

client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(worker_ProgressChanged);


// (sender as BackgroundWorker).ReportProgress((client.DownloadFileAsync( new Uri(yo), FileName)));
worker.ReportProgress(10);
}

}

private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{


MessageBox.Show("I am reporting");
}
Posted
Updated 25-Jul-16 16:01pm

1 solution

The following code will help you:

C#
WebClient webClient = new WebClient();
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webClient_DownloadProgressChanged);
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadFileCompleted);
webClient.QueryString.Add("file", localFilePath); // To identify the file 
webClient.DownloadFileAsync("URL here", localFilePath);


And the webClient_DownloadProgressChanged event will be like the following:

C#
void webClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
    string fileProcessed = ((System.Net.WebClient)(sender)).QueryString["file"]; // Getting the local path if required       
    var RecevedBytes = e.BytesReceived;
    var TotalBytes = e.TotalBytesToReceive;
    // use these variables if needed
    bar.Value = e.ProgressPercentage;        

}
 
Share this answer
 
Comments
Member 11649479 26-Jul-16 13:03pm    
Sujith unfortunately its not working. Its jumping straight to DownloadFIleCompleter, not even downloading anything, not giving me any errors.
private void button1_Click(object sender, EventArgs e)
{

var urlInks = listBox1.Items.Cast<string>().ToList();

List<string> myList = (List<string>)urlInks;
string localFilePath = "C:\\Test4\\";
WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadFileCompleted);
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webClient_DownloadProgressChanged);
// To identify the file

webClient.QueryString.Add("file", localFilePath);
webClient.DownloadFileAsync(new Uri("http://ligman.me/29ngkYn"), localFilePath);

}


public void webClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
MessageBox.Show("Reporting Progress");
// string fileProcessed = ((System.Net.WebClient)(sender)).QueryString["file"]; // Getting the local path if required
var RecevedBytes = e.BytesReceived;
var TotalBytes = e.TotalBytesToReceive;
// use these variables if needed
progressBar1.Value = e.ProgressPercentage;

}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900