This code seems to have worked for me.
We would have to wait for humankind to get super smarter so we don't get a programming machine that doesn't dispose its object with "using() {}" as above.
void myDownloadFileAsyn(string s)
{
while(currentNumberOfDownloads>30)
{
System.Threading.Thread.Sleep(350);
}
BackgroundWorker bgw = new BackgroundWorker();
bgw.DoWork += Bgw_DoWork;
bgw.RunWorkerCompleted += Bgw_RunWorkerCompleted;
bgw.RunWorkerAsync(argument:s);
}
private void Bgw_DoWork(object sender, DoWorkEventArgs e)
{
string s = e.Argument as string;
WebClient wc = new WebClient();
currentNumberOfDownloads++;
wc.DownloadFile(s, @"g:\folder\" + s.Substring(s.LastIndexOf(@"\")+1));
currentNumberOfDownloads--;
wc.Dispose();
}
private void Bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
(sender as BackgroundWorker).Dispose();
}
int currentNumberOfDownloads = 0;