网页信息采集,
第一层:1个URL
第二层:10个URL,保存在listview1
第三层:100个URL,保存在listview2
---------------------------------------------
现在我要对这些URL进行源代码获取,并把每个URL对应的源代码保存到数据库里,
---------------------------------------------
private string GetHTML(string url)
{

System.Net.WebClient client = new System.Net.WebClient();

try
{
byte[] buf  = client.DownloadData(url);
return System.Text.Encoding.Default.GetString(buf);
}
catch( Exception er)
{
MessageBox.Show(er.ToString());
}
return null;
            
}
---------------------------------------------
请问怎么样用多线程来实现,谢谢!

解决方案 »

  1.   

    首先,线程数太多不好,所以不能设定诸如10个URL一个线程的算法。应该有一个最多线程数的上限,例如5个线程。然后,你就可以把URL平均分配给各个线程处理了。第一层,一个URL,只用一个线程就可以。第二层,10个URL,每个线程2个URL。第三层,100个URL, 每个线程20个URL。
      

  2.   

    我看了一下,不管是第几层URL,做得是清都是一样的,所以可以用线程池的模型去做,初始化固定数量的工作线程,通过一个任务队列驱动。
      

  3.   

    呵呵!能不能给出具体的代码啊?
    我是初学的,所以很难理解啊
    ------------------------------------------
    for(int i=0;i<listView1.Items.Count;i++)
       {
       for(int x=0;x<listView1.Columns.Count;x++)
          {   this.GetHTML(listView1.Items[i].SubItems[2].Text.Trim());// 这里就是我要使用多线程的地方,我从listview1,里面循环读出的URL地址。然后对每个URL采集源代码
          }
      }
    ------------------------------------------
      

  4.   

    我看了下MSDN上的例子,那个好像简单了点啊,用不上啊
    using System;
    using System.Threading;
    public class ThreadExample 
    {
    public static void ThreadProc() 
    {
    for (int i = 0; i < 20; i++) 
    {
    Console.WriteLine("ThreadProc: {0}", i);
    Thread.Sleep(0);
    }
    } public static void Main() 
    {
    Console.WriteLine("Main thread: Start a second thread.");
    Thread t = new Thread(new ThreadStart(ThreadProc));
    t.Start();
    for (int i = 0; i < 4; i++) 
    {
    Console.WriteLine("Main thread: Do some work.");
    Thread.Sleep(0);
    } Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.");
    t.Join();
    Console.WriteLine("Main thread: ThreadProc.Join has returned.  Press Enter to end program.");
    Console.ReadLine();
    }
    }
      

  5.   

    Thread.Sleep(0)//有什么现实意义吗
      

  6.   

    public class MyDownload
    {
       public Thread t;
       private string URL;
    public void ThreadProc() 
    {
    GetHTML(url ....);
    ...........
    } public MyDownload(string url)
    {
       this.URL = url;
       t= new Thread(new ThreadStart(this.ThreadProc));
       t.Start();
    }




    }
      

  7.   

    for(int i=0;i<listView1.Items.Count;i++)
       {
       for(int x=0;x<listView1.Columns.Count;x++)
          {   new MyDownload(url);
          }
      }