Ejemplo n.º 1
0
 public static void DoWork(object data)
 {
     try
     {
         CollectorThread cThread = (CollectorThread)data;
         //采集主体
         Collector collector = cThread.Collector;
         //即将访问的URL队列
         UrlQueue urlQueue = collector.urlQueue;
         while (true)
         {
             if (urlQueue.Count > 0)
             {
                 try
                 {
                     // 从队列中获取URL
                     string url = (string)urlQueue.Dequeue();
                     // 获取页面
                     cThread.Url = url;
                     if (cThread.Dirty)
                     {
                         cThread.CollectotThreadCallbacked(collector);
                     }
                     string html = HtmlHelper.GetHtml(url, "UTF-8");
                     //检索页面上的邮件
                     CollectHelper.CollectEmail(html);
                     if (cThread.Dirty)
                     {
                         cThread.CollectotThreadCallbacked(collector);
                     }
                 }
                 catch (InvalidOperationException)
                 {
                     SleepWhenQueueIsEmpty(cThread);
                 }
             }
             else
             {
                 SleepWhenQueueIsEmpty(cThread);
             }
         }
     }
     catch (ThreadAbortException)
     {
         // 线程被放弃
     }
 }
Ejemplo n.º 2
0
 /// <summary>
 /// 开始线程
 /// </summary>
 /// <param name="data"></param>
 public static void DoWork(object data)
 {
     try
     {
         LinkerThread LinkerThread = (LinkerThread)data;
         //主体
         Linker Linker = LinkerThread.Linker;
         //即将访问的URL队列
         UrlQueue urlQueue = Linker.urlQueue;
         while (true)
         {
             if (urlQueue.Count > 0)
             {
                 try
                 {
                     // 从队列中获取URL
                     string url = (string)urlQueue.Dequeue();
                     // 获取页面
                     LinkerThread.Url = url;
                     if (LinkerThread.IsComplete)
                     {
                         LinkerThread.LinkerThreadStatusChanged(Linker);
                     }
                     string html = HtmlHelper.GetHtml(url, "UTF-8");
                     LinkHelper.CollectEmail(url, html);
                     if (LinkerThread.IsComplete)
                     {
                         LinkerThread.LinkerThreadStatusChanged(Linker);
                     }
                 }
                 catch (InvalidOperationException)
                 {
                     SleepWhenQueueIsEmpty(LinkerThread);
                 }
             }
             else
             {
                 SleepWhenQueueIsEmpty(LinkerThread);
             }
         }
     }
     catch (ThreadAbortException)
     {
         // 线程被放弃
     }
 }