网页信息采集,
第一层: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个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;
}
---------------------------------------------
请问怎么样用多线程来实现,谢谢!
解决方案 »
- 自定义gridview属性
- cs系统C#开发窗体与窗体之间相互控制的问题
- 如果用DataTable读取dataGird的数据该怎样读呢
- c#写的软件,用户的机器上如果没有安装.Net框架能够正常使用吗?
- 一个datagrid判断显示的问题 不知道怎么下手 100分
- 我的.net 2003出问题了!
- .net remoting 的问题
- 请教哪位高手有Anakrino9所配合的IIL中间语言参考手册?给100分!!
- C#中如何调用存储过程(在线等)
- 非静态的字段、方法或属性“System.Configuration.Install.Installer.Installers????
- 怎么在程序退出前 相应某事件
- 请问:如何限制winform程序的cpu使用率 ,在线等待
我是初学的,所以很难理解啊
------------------------------------------
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采集源代码
}
}
------------------------------------------
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();
}
}
{
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();
}
}
{
for(int x=0;x<listView1.Columns.Count;x++)
{ new MyDownload(url);
}
}