C#-使用WebClient的多线程

时间:2018-09-15 19:05:14

标签: c# multithreading web

我想问问是否可以使用WebClient进行多线程处理?

Parallel.ForEach(Keywords, s =>
        {
            for (int i = 0; i < Pages; i++)
            {
                string url = URLdownloader.DownloadString("https://www.bing.com/search?q=" + s + "&first=" + i + "0");
                MatchCollection urlCollection = Regex.Matches(url.ToString(), @"(\Whttp:\/\W[a-z./?=A-Z0-9&-]+)");
                foreach (Match m in urlCollection)
                {
                    urlsdownload.Add(m.ToString().Replace(@"""", ""));
                    urlsdownload = urlsdownload.Distinct().ToList();
                    File.WriteAllLines(
                        @"C:\Users\username\Desktop\DorkWorker\URLparser\Bing.txt".Replace("username",
                            Environment.UserName), urlsdownload);
                }
            }
        });

假设我有100个网址,而不是一一检查一遍,怎么可能同时检查十个呢?还是5?

1 个答案:

答案 0 :(得分:0)

您应该使用DownloadStringAsync()而不是DownloadString: int curDownload = 0; 假设您的网址位于名为DownloadUrls的字符串数组中:

WebClient[] wc = new WebClient[10];
for(int i=0;i<10;i++)
{
   wc[i] = new WebClient();
   wc[i].DownloadStringCompleted += (s, e) => 
   {
         if(curDownload<DownloadUrls.Length-1)
         (WebClient(s)).DownloadStringAsync(DownloadUrls[curDownload++]);
   };
if(curDownload<DownloadUrls.Length-1)
wc[i].DownloadStringAsync(DownloadUrls[curDownload++]);
}