Multi-threaded Application One

Source: Internet
Author: User

1. Image download

1   Public class Program2     {3         Static voidMain (string[] args)4         {5             string[] str = {"Model","Sexy","Belle","stars" };6              for(intURL =0; URL < str. Length; url++)7             {8Thread thread =NewThread (DownLoad);9 Ten thread. Start (Str[url]); One             } A Console.read (); -         } -  the          Public Static voidDownLoad (Objectcategory) -         { -             stringURL =string. Empty; -  +              for(intPurl =9014; Purl >Ten; purl--) -             { +                  for(intPageSize =0; PageSize < -; pagesize++) A                 { at                     Try -                     { -                         if(PageSize = =0) -URL ="http://www.mm8mm8.com/"+ Category +"/"+ Purl +". html"; -                         Else -URL ="http://www.mm8mm8.com/"+ Category +"/"+ Purl +"_"+ PageSize +". html"; in  -                         //Create an HTTP link to                         varRequest =(HttpWebRequest) webrequest.create (URL); +  -Request. Timeout = +*5;//5s Expired the  *                         varResponse =(HttpWebResponse) request. GetResponse (); $ Panax NotoginsengStream stream =Response. GetResponseStream (); -  theStreamReader sr =NewStreamReader (stream); +  A                         stringContent =Sr. ReadToEnd (); the  +                         varList =gethtmlimageurllist (content); -  $WebClient client =NewWebClient (); $  -  -                         varDirectoryName =@"C:\down\"; the  -                         if(!directory.exists (directoryname))Wuyi directory.createdirectory (directoryname); the  -                         varFileName =string. Empty; Wu  -                         if(list. Count = =0) About                         { $Console.WriteLine ("Time:"+ DateTime.Now +"Current URL:"+ URL +"no pictures found"); -                              Break; -                         } -  A                         Try +                         { the  -FileName = category +"_"+ Purl +"_"+ (PageSize +1) +". jpg"; $  the                             varLocalFile = DirectoryName +FileName; the  the                             varImagerequest = (HttpWebRequest) webrequest.create (list[0]); the  -Imagerequest.timeout = +*5;//5s Timeout in  the                             varImageresponse =(HttpWebResponse) imagerequest.getresponse (); the  About                             vars =Imageresponse.getresponsestream (); the  theImage image =Image.fromstream (s); the  + image. Save (localfile); -  the image. Dispose ();Bayi  theConsole.WriteLine ("Time:"+ DateTime.Now +"Photo:"+ FileName +"download saved to disk location:"+localfile); the  -                         } -                         Catch(Exception e) the                         { theConsole.WriteLine ("Time:"+ DateTime.Now +"Current Picture:"+ FileName +"error message:"+e.message); the                             Continue; the                         } -                     } the                     Catch(Exception ex) the                     { theConsole.WriteLine ("Time:"+ DateTime.Now +"Current URL:"+ URL +"error message:"+Ex. Message);94                     } the                 } the             } the         }98  About         /// <summary>  -         ///gets the URL of all the pictures in the HTML. 101         /// </summary> 102         /// <param name= "Shtmltext" >HTML code</param> 103         /// <returns>List of URLs for pictures</returns> 104          Public Staticlist<string> Gethtmlimageurllist (stringshtmltext) the         {106             //define a regular expression to match an IMG tag107Regex regimg =NewRegex (@"]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*["'"? [\s\t\r\n]* (? [^\s\t\r\n "" ' <>]* ") [^<>]*?/? [\s\t\r\n]*>", regexoptions.ignorecase);108 109             //search for matching strings theMatchCollection matches =regimg.matches (shtmltext);111  thelist<string> surllist =Newlist<string>();113  the             //get a list of matches the             foreach(Match matchinchmatches) theSurllist.add (match. groups["Imgurl"]. Value);117             returnsurllist;118         }119}
View Code

Multi-threaded Application One

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.