I need to create multiple file downloaders using WebClient
and threading, for learning purposes I created this code (below), the file is downloaded correctly but sometime the download stops and doesn't start running again. I also want to control the ProgressBar
. Invoker of this code is a button.
for (int i = 0; i < 3; i++)
{
Start(
delegate
{
WebClient wc = new WebClient();
if(wc.IsBusy != true)
{
wc.DownloadFileAsync(new Uri("http://ipv4.download.thinkbroadband.com/10MB.zip"), @"D:\File" + Convert.ToString(i) + ".txt");
wc.DownloadProgressChanged +=
delegate(object sender1, DownloadProgressChangedEventArgs e1) {
foreach (ToolStripProgressBar it in statusStrip1.Items)
{
if ((it.Name == "pg" + Convert.ToString(x)) == true)
{
it.Control.Invoke((MethodInvoker)delegate() { it.Value = e1.ProgressPercentage; });
}
}
};
}
});
}
public static Thread Start(Action action)
{
Thread thread = new Thread(() => { action(); });
thread.Start();
return thread;
}