Winform: Program for collecting beautiful pictures on websites-multi-thread articles, winform beautiful pictures
The previous collection program is a single thread, a list of images, waiting to be completed one by one .... A lot of time is wasted, just as the bandwidth upgrade at home today can be collected using multiple threads .... overnight improvements to the original program. use multiple threads to collect data ....
Set the idea: collection target: http://www.8kmm.com, known url List (List save), application multithreading (Thread) read the List, get url cannot be repeated (Lock). Allow unordered collection!
Let's take a look!
Multi-threaded core code:
1 # region global variable 2 // Thread List 3 List <Thread> threadslList = new List <Thread> (); 4 // Url List 5 List <string> uUrls = new List <string> (); 6 // processed List 7 List <string> OkUrls = new List <string> (); 8 // number of successful beauty pictures 9 public int ImgCount = 0; 10 # endregion 11 12 13 // figure fetch start button event 14 private void lbtngetwebimstart_linkclicked (object sender, LinkLabelLinkClickedEventArgs e) 15 {16 lbtnGetWebImgStart. enabled = false; 1 7 GetWebSiteImg (); 18} 19 20 // screenshot stop button event 21 private void lbtngetwebimstop_linkclicked (object sender, LinkLabelLinkClickedEventArgs e) 22 {23 try 24 {25 foreach (Thread t in threadslList) 26 {27 if (t! = Null) 28 {29 t. abort (); 30} 31} 32} 33 catch (Exception ex) 34 {35 WriteLog ("Stop failed:" + ex. message); 36} 37 finally 38 {39 lbtnGetWebImgStart. enabled = true; 40} 41} 42 43 // <summary> 44 // Method for retrieving the image master 45 /// </summary> 46 private void GetWebSiteImg () 47 {48 try 49 {50 ImgCount = 0; 51 OkUrls. clear (); 52 uUrls. clear (); 53 threadslList. clear (); // initialize the preceding 54 string [] urls = txtUrl. text. split (new string [] {"\ r \ n"}, StringSplitOptions. removeEmptyEntries); 55 lblC. text = urls. length. toString (CultureInfo. invariantCulture); 56 uUrls = new List <string> (urls); 57 for (int I = 0; I <int. parse (numThreadUD. text); I ++) // loop creation Thread 58 {59 Thread t = new Thread (Task); 60 t. name = I. toString (); 61 t. isBackground = true; 62 threadslList. add (t); 63 t. start (); 64} 65} 66 catch (Exception ex) 67 {68 WriteLog (ex. message); 69} 70} 71 72 73 public void Task () 74 {75 while (uUrls. count> 0) 76 {77 lblMsg. text = string. format ("remaining: {0}, accessed: {1}, current thread count: {2}", uUrls. count, (OkUrls. count + 1), threadslList. count); 78 string url = GetUrl (); 79 if (url = "") 80 {81 break; 82} 83 else 84 {85 if (GetHttpImg (url) = 200) // only status 200 is normal. GetHttpImg is my encapsulated method. Get the webpage and obtain all the images in combination with the regular expression. 86 {87 OkUrls. add (url); 88 lblInfoStart. text = (int. parse (lblInfoStart. text) + 1 ). toString (CultureInfo. invariantCulture); 89 WriteHtml (txtUrl. text); 90 if (lblC. text = lblInfoStart. text) // determine whether the current number is the total number. 91 {92 lblMsg. text = "complete"; 93} 94} 95} 96} 97} 98 99 100 // <summary> 101 // lock the thread, prevent multiple threads from simultaneously extracting the first 102 from the list /// </summary> 103 /// <returns> </returns> 104 public string GetUrl () 105 {106 lock ("GetUrl") 107 {108 if (uUrls. count> 0) 109 {110 string url = uUrls [0]; 111 uUrls. removeAt (0); 112 return url; 113} 114 else115 {116 return ""; 117} 118} 119}
The code is super simple. The test captures several lists. There is no problem. It will be used to capture more MM...