http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 2927|回复: 1

[其他] 苏老师。帮忙修改下这个多线程~~

[复制链接]
发表于 2013-10-23 11:48:16 | 显示全部楼层 |阅读模式
启动:
  1. OutputInfo("开始启动操作线程...");
  2.             for (int i = 0; i <= ThreadNumber; i++)//5个线程
  3.             {
  4.                 ThreadStart threadStart = new ThreadStart(StartGo);
  5.                 thread = new Thread(threadStart);
  6.                 thread.Name = i.ToString();
  7.                 ThL.Add(thread);
  8.                 thread.Start();
  9.             }
复制代码
函数:
  1. private void StartGo()
  2.         {
  3.             string URL = "";
  4.             string proxy = "";
  5.             string htmlcode = "";
  6.             while (Proxylist.Count != 0)
  7.             {
  8.                 for (int i = 0; i < Proxylist.Count; i++)//循环代理
  9.                 {
  10.                     

  11.                     lock (tLock)
  12.                     {
  13.                         if (URL == "")
  14.                         {
  15.                             URL = Linkslist[0];//链接列表
  16.                             Linkslist.Remove(URL);
  17.                         }


  18.                        proxy = Proxylist[0];//代理列表
  19.                        Proxylist.Remove(proxy);
  20.                         Proxy_label.Text = Proxylist.Count.ToString();
  21.                     }//loo
  22.                         htmlcode = GetHtmlCode(URL, proxy);

  23.                         OutputInfo("线程[ " + Thread.CurrentThread.Name + " ] 操作" + proxy + " / " + GetViewkey(URL));

  24.                         if (htmlcode.Length < 30)
  25.                         {
  26.                             OutputInfo("错误:" + proxy + " > " + htmlcode);
  27.                            
  28.                             proxy = "";

  29.                         }
  30.                         else if (GetContent(1, htmlcode) == "")
  31.                         {
  32.                             OutputInfo("错误:" + proxy + "失效,继续下一个代理! > ");
  33.                         
  34.                             proxy = "";
  35.                         }
  36.                         else
  37.                         {
  38.                             ModData(GetContent(1, htmlcode), GetContent(0, htmlcode), GetViewkey(htmlcode).Trim());//存入数据库

  39.                             proxy = "";
  40.                            // Linkslist.Remove(URL);
  41.                             LinksData_label.Text = Linkslist.Count.ToString();
  42.                             URL = "";
  43.                         }
  44.                      
  45.                   

  46.                 }//for
  47.               
  48.             }//w
  49.             OutputInfo("代理地址已经使用完!");
  50.         }
复制代码
程序运行后。
前5个线程和下5个线程相隔有半分钟甚至更长。是我代码哪里的问题啊。帮忙修改下。

我的思路是:
循环代理地址取网页代码。成功换代理及下一个网页。不成功换代理继续这个网页


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2013-10-23 12:43:59 | 显示全部楼层
[code=csharp]Thread objThread = new Thread(new ThreadStart(delegate
            {
                ThreadMethodTxt(textBox1.Text.Trim());
            }));
            objThread.Start();[/code]

建议使用这种方法启动线程
在启动线程时有个时间间隔,最好设置为20毫秒左右,不要一点间隔也不有,这样可能有些线程就启动不了。或者会反映过慢, 具体的可以参数我的例子
http://www.sufeinet.com/forum.ph ... ;tid=13&ctid=26
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-5-3 14:28

© 2014-2021

快速回复 返回顶部 返回列表