How to download multiple files in C# from array. | | | Join Date: Jan 2010
Posts: 8
| |
I have an array with filenames, and i want to download them from the server, but once i start a download, it must finish before i star a new one. I don't know how to make it wait, provided that the loop is inside a Download_COmpleted event function...
Here is the code: - while (l <= j-1)
-
{
-
label1.Text = "Baixando " + download[l] + "...";
-
client2.DownloadFileAsync(new Uri(url + download[l]), download[l]);
-
client2.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
-
l++;
-
}
| - while (l < i)
-
{
-
label1.Text = "Baixando " + download[l] + "...";
-
client2.DownloadFileAsync(new Uri(url + download[l]), download[l]);
-
client2.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
-
while (progressBar1.Value < 100)
-
{
-
Application.DoEvents();
-
label4.Text = progressBar1.Value.ToString() + " %";
-
}
-
l++;
-
progressBar1.Value = 0;
-
}
Did you realize that every time you go through the outter loop (L) that you are attaching another response to the client2.DownloadProgressChanged event? (Line 5)
The first time through the outer loop the handler will be attached (+=) and so the method will be called one time for every DownloadProgressChanged event.
The second time through the outer loop the same method will be attached to the same event AGAIN. So when the DownloadProgressChanged event is raised the handler method will be notified TWICE.
and so on. Maybe the weird behavior you are seeing is because the file isn't done actually done downloading when the progressbar reaches a value of 100
RULE: For every attachment (+=) of an event to a method handler you must also detach (-=) the handler from the event. If you don't you will get unexpected results and it will slowly consume memory. Disposing of a class without detaching event handlers will create a memory leak because their is no longer a way to reference the connection to disconnect it.
Try something like this - client2.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
-
// Move the attachment to before the loop so it only happens once.
-
while (l < i)
-
{
-
label1.Text = "Baixando " + download[l] + "...";
-
client2.DownloadFileAsync(new Uri(url + download[l]), download[l]);
-
while (progressBar1.Value < 100)
-
{
-
Application.DoEvents();
-
label4.Text = progressBar1.Value.ToString() + " %";
-
}
-
l++;
-
progressBar1.Value = 0;
-
}
-
client2.DownloadProgressChanged -= new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
-
// Detach the handler from the event when you are done with it.
-
 | | | Join Date: Mar 2008 Location: Arizona, USA
Posts: 3,476
| | | re: How to download multiple files in C# from array.
I am WAY out of my area here but...
Line 4 looks like an Asynchronous download method.
Is there one just like it that doesn't have the Aysnc? Just "DownloadFile"
| | | | Join Date: Jan 2010
Posts: 8
| | | re: How to download multiple files in C# from array.
Yes, but that one doesn't connect with the Async progress bar...
|  | | | Join Date: Mar 2008 Location: Arizona, USA
Posts: 3,476
| | | re: How to download multiple files in C# from array.
but once i start a download, it must finish before i star a new one.
Yes, but that one doesn't connect with the Async progress bar...
Then use a different progress bar.
|  | | | Join Date: Apr 2007 Location: New England
Posts: 7,492
| | | re: How to download multiple files in C# from array.
Have you tried creating a new webclient object?
The Async part allows you to download without blocking the thread, so make another one and try downloading a 2nd file.
If it works (and doesn't cause memory/io issues) expand from there.
I would not recomend doing more then 2 downloads at once though
| | | | Join Date: Jan 2010
Posts: 8
| | | re: How to download multiple files in C# from array.
No, i don't want to do them at the same time, i want to do them one after one.
|  | | | Join Date: Apr 2007 Location: New England
Posts: 7,492
| | | re: How to download multiple files in C# from array.
Well ok I misread it, then whats the issue?
When the download completed event is fired, start the next one?
| | | | Join Date: Jan 2010
Posts: 8
| | | re: How to download multiple files in C# from array.
It is not suposed to, but sometimes, at random, it ignores the loop in the end and start a new download while another one is in progress and craches the program. - while (l < i)
-
{
-
label1.Text = "Baixando " + download[l] + "...";
-
client2.DownloadFileAsync(new Uri(url + download[l]), download[l]);
-
client2.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
-
while (progressBar1.Value < 100)
-
{
-
Application.DoEvents();
-
label4.Text = progressBar1.Value.ToString() + " %";
-
}
-
l++;
-
progressBar1.Value = 0;
-
}
Any sugestion?
|  | | | Join Date: Apr 2007 Location: New England
Posts: 7,492
| | | re: How to download multiple files in C# from array.
Why are you not doing the progressbar stuff in the downloadprogresschanged event?
You probably should not use a while loop then, but instead pass the index around in the event handlers and when the download complete happens, you can pass the next index into a download again
| | | | Join Date: Jan 2010
Posts: 8
| | | re: How to download multiple files in C# from array.
But the while loop is already inside the Download Completed Event.
The dowloads loop occurs after another one.
|  | | | Join Date: Mar 2008 Location: Arizona, USA
Posts: 3,476
| | | re: How to download multiple files in C# from array. - while (l < i)
-
{
-
label1.Text = "Baixando " + download[l] + "...";
-
client2.DownloadFileAsync(new Uri(url + download[l]), download[l]);
-
client2.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
-
while (progressBar1.Value < 100)
-
{
-
Application.DoEvents();
-
label4.Text = progressBar1.Value.ToString() + " %";
-
}
-
l++;
-
progressBar1.Value = 0;
-
}
Did you realize that every time you go through the outter loop (L) that you are attaching another response to the client2.DownloadProgressChanged event? (Line 5)
The first time through the outer loop the handler will be attached (+=) and so the method will be called one time for every DownloadProgressChanged event.
The second time through the outer loop the same method will be attached to the same event AGAIN. So when the DownloadProgressChanged event is raised the handler method will be notified TWICE.
and so on. Maybe the weird behavior you are seeing is because the file isn't done actually done downloading when the progressbar reaches a value of 100
RULE: For every attachment (+=) of an event to a method handler you must also detach (-=) the handler from the event. If you don't you will get unexpected results and it will slowly consume memory. Disposing of a class without detaching event handlers will create a memory leak because their is no longer a way to reference the connection to disconnect it.
Try something like this - client2.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
-
// Move the attachment to before the loop so it only happens once.
-
while (l < i)
-
{
-
label1.Text = "Baixando " + download[l] + "...";
-
client2.DownloadFileAsync(new Uri(url + download[l]), download[l]);
-
while (progressBar1.Value < 100)
-
{
-
Application.DoEvents();
-
label4.Text = progressBar1.Value.ToString() + " %";
-
}
-
l++;
-
progressBar1.Value = 0;
-
}
-
client2.DownloadProgressChanged -= new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
-
// Detach the handler from the event when you are done with it.
-
|  | | | | |