I want to download more than one file by using webclient method and many threads running at the same time. My url structure depends on a variable ‘int i’, so i use a for loop to generate urls and filepaths. The problem is until the started thread is brought upon, the url and filepath values are changed. The timeline occurs as below:
In main loop, url = “url1” and path = “filepath1”.
Thread1 is called with value “url1” and “filepath1”.
In main loop, url = “url2” and path = “filepath2”.
Thread2 is called with value “url2” and “filepath2”.
Thread1 started with value “url2” and “filepath2”.
Thread2 started with value “url2” and “filepath2”.
I couldn’t find any elegant solutions. What would you suggest?
string path = "";
string url = "";
string baseURL = "http://www.somewebsite.com/12/";
for (int i = 10; i <= DateTime.Now.Month; i++)
{
path = "C:\\folder\\" + i.ToString() + ".html";
url = baseURL + i.ToString();
Thread webThread = new Thread(delegate()
{
downloadScheduleFile(url,path);
});
webThread.Start()
}
private void downloadScheduleFile(string url, string filepath)
{
var client = new WebClient();
try
{
client.DownloadFile(url, filepath);
}
catch(WebException e) {
Console.WriteLine(System.Threading.Thread.CurrentThread.Name+e.Message);
}
}
Its because by the time your thread starts,
pathandurlhave changed. You have to create closer local copies.