I am trying to create an application that multithreaded downloads images from a website as an introduction to streams. (never used thread before)
But at present it seems that 1000+ threads are being created, and I'm not sure where they are coming from.
First I put the thread queue in the thread pool, for starters I only have 1 job in the job array
foreach (Job j in Jobs) { ThreadPool.QueueUserWorkItem(Download, j); }
What launches void Download(object obj) in a new stream, where it goes through a certain number of pages (images are needed / 42 images per page)
for (var i = 0; i < pages; i++) { var downloadLink = new System.Uri("http://www." + j.Provider.ToString() + "/index.php?page=post&s=list&tags=" + j.Tags + "&pid=" + i * 42); using (var wc = new WebClient()) { try { wc.DownloadStringAsync(downloadLink); wc.DownloadStringCompleted += (sender, e) => { response = e.Result; ProcessPage(response, false, j); }; } catch (System.Exception e) {
correct me if i am wrong the next void is called in the same thread
void ProcessPage(string response, bool secondPass, Job j) { var wc = new WebClient(); LinkItem[] linkResponse = LinkFinder.Find(response).ToArray(); foreach (LinkItem i in linkResponse) { if (secondPass) { if (string.IsNullOrEmpty(i.Href)) continue; else if (i.Href.Contains("http://loreipsum.")) { if (DownloadImage(i.Href, ID(i.Href))) j.Downloaded++; } } else { if (i.Href.Contains(";id=")) { var alterResponse = wc.DownloadString("http://www." + j.Provider.ToString() + "/index.php?page=post&s=view&id=" + ID(i.Href)); ProcessPage(alterResponse, true, j); } } } }
And finally it goes to the last function and loads the actual image
bool DownloadImage(string target, int id) { var url = new System.Uri(target); var fi = new System.IO.FileInfo(url.AbsolutePath); var ext = fi.Extension; if (!string.IsNullOrEmpty(ext)) { using (var wc = new WebClient()) { try { wc.DownloadFileAsync(url, id + ext); return true; } catch(System.Exception e) { if (DEBUG) Debug.Log(e); } } } else { Debug.Log("Returned Without a extension: " + url + " || " + fi.FullName); return false; } return true; }
I'm not sure how I start this many threads, but would like to know.
Edit
The goal of this program is to load one task at a time (maximum 5), each of which loads a maximum of 42 images.
so a maximum of 210 images can / should be uploaded at most at any time.