The conception: I'm making a C# app which downloads files from a given URL. Textbox, url added, file downloads, every event occurs in the correct way.
I'm trying to recreate this program to download multiple files, one by one. I have a textbox with one url/line, parsing happens correctly, I have all the links in a string array which was placed in the textbox. Then it starts downloading async, and I wanted to make it download only one by one, so I made a while loop in a foreach loop, as I don't want to go to the next url until the current one has finished downloading.
The problem is: I get in an infinite loop (though I made this work before (idk how), if I placed a messagebox in the while loop (note: I retried a minute ago, it didn't do the trick this time)).
I'll just show the code snippet:
foreach (string url in urllist)
{
isdonwloaded = false;
string filename = url.Split('/').Last();
label3.Text = filename;
webclient.DownloadFileAsync(new Uri(url), @"C:\Users\Krisz" + @"\" + filename);
while (!isdonwloaded) // this was the first idea, but with webclient.IsBusy it did the same thing
{
// MessageBox.Show(counter);
Thread.Sleep(1000);
label8.Text = "Download in progress...";
}
counter++;
label8.Text = "Done!";
}
// Events:
webclient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webc_DownloadProgressChanged);
webclient.DownloadFileCompleted += new AsyncCompletedEventHandler(webc_DownloadFileCompleted);
// The DownloadFileCompleted event:
void webc_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
label7.Text = String.Format("Files {0} / {1}", counter, arraylength(urllist));
isdonwloaded = true;
}
I have looked into this thread: WebClient.DownloadFileAsync - Download files one at a time , but I couldn't manage to make it work either that way. (Maybe I misunderstood something?)
Can someone give me some hints, what did I do wrong? I never really used events, so it was only matter of time to run into an error. Every bit of help is greatly appreaciated, this program would be a useful one for me.