|
发表于 2013-6-15 11:17:46
|
显示全部楼层
[code=csharp]public partial class MainFrm : Form
{
public MainFrm()
{
InitializeComponent();
}
System.Threading.Thread t;
private delegate void VoidDelegate();
private void MainFrm_Load(object sender, EventArgs e)
{
t = new System.Threading.Thread(new System.Threading.ThreadStart(this.gethtml));
}
private void button1_Click_1(object sender, EventArgs e)
{
if (t.ThreadState == System.Threading.ThreadState.Unstarted)
{
t.Start();
this.button1.Text = "暂停";
}
else
{
if (t.ThreadState == System.Threading.ThreadState.Suspended)
{
t.Resume();
this.button1.Text = "暂停";
}
else
{
t.Suspend();
this.button1.Text = "继续";
}
}
}
private void MainFrm_FormClosed(object sender, FormClosedEventArgs e)
{
if (t.ThreadState == System.Threading.ThreadState.Suspended)
{
t.Resume();
t.Abort();
}
}
private void gethtml()
{
HttpHelper http = new HttpHelper();
HttpItem item = null;//创建HTTP访问类对象
item = new HttpItem()
{
URL = "http://news.sohu.com/1/0903/61/subject212846158.shtml",//URL 必需项
Method = "get",//URL 可选项 默认为Get
Cookie = ""//当前登录Cookie
};
HttpResult result = http.GetHtml(item);
Regex reg = new Regex(@"·<a href=(?<url>([^\""""\'>\s]*)) target=_blank>(?<title>([^<]+|.*?)?)</a><span>");
MatchCollection mc = reg.Matches(result.Html);
foreach (Match m in mc)
{
string url = m.Groups["url"].Value;
string title = m.Groups["title"].Value;
this.BeginInvoke(new VoidDelegate(delegate(){
textBox1.AppendText("标题为:" + title + "\r\n网址为:" + url + "\r\n\r\n");
}));
System.Threading.Thread.Sleep(1000);
}
}
}[/code]
多线程版 |
|