SpiderForm.cs
SpiderForm.cs
一、单击开始按钮
启动线程spider,其调用方法为SpiderThread()
二、SpiderThread()方法
如果按钮begin的Text值等于“取消”,则
m_spider.Quit=true(m_spider是Spider类的一个实例)
按钮begin的Enabled值为“false”
否则
按钮begin的Text值为“取消”
文本框targetURL的Enabled值为“false”
文本框threadCount的Enabled值为“false”
文本框outputDir的Enabled值为“false”
创建一个Spider类的实例,其引用为m_spider
m_spider.ReportTo = this;
m_spider.Report的值为m_spider实例
(在类的构造函数中出现的this作为一个值类型,它表示对正在构造的对象本身的引用
在类的方法中出现的this作为一个值类型,表示对调用该方法的对象的引用
在结构的构造函数中出现的this作为一个变量类型,表示对正在构造的结构的引用
在结构的方法中出现this作为一个变量类型,表示对调用该方法的结构的引用 )
将outputDir.Text赋值给实例m_spider的属性OutputPath
将threadCount.Text转换成整数赋值给threads
如果threads<1,则threads=1
将threads赋值给threadCount.Text
尝试启动m_spider.Start(new Uri(this.targetURL.Text),threads),其输入参数为URL,threads
捕捉到错误,显示错误并返回
恢复系统环境
begin.Text = "Begin";
targetURL.Enabled = true;
threadCount.Enabled = true;
outputDir.Enabled = true;
begin.Enabled = true;
三、SetLastURL(string str)方法
当前的URL
currentURL.Text = str
四、 SetElapsedTime(string str)方法
用时
elapsed.Text = str
五、SetProcessedCount(string str)方法
已经处理的URL数量
processedURLs.Text = str
六、SpiderForm_Close(object sender, CancelEventArgs e)方法
退出程序
Application.Exit()
源代码:
public class SpiderForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox targetURL;
private System.Windows.Forms.TextBox threadCount;
private System.Windows.Forms.Button begin;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Label currentURL;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Label processedURLs;
private System.Windows.Forms.Label elapsed;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox outputDir;
private Spider m_spider;
...
private void begin_Click(object sender, System.EventArgs e)
{
//启动一个线程
ThreadStart starter = new ThreadStart(this.SpiderThread);
Thread spider = new Thread(starter);
spider.Start();
}
public void SpiderThread()
{
if( begin.Text.Equals("Cancel") )
{
m_spider.Quit = true;
begin.Enabled = false;
}
else
{
begin.Text = "Cancel";
targetURL.Enabled = false;
threadCount.Enabled = false;
outputDir.Enabled = false;
//开始一个Spider
m_spider = new Spider();
m_spider.ReportTo = this;
m_spider.OutputPath = outputDir.Text;
int threads = int.Parse( threadCount.Text);
if(threads<1)
threads = 1;
threadCount.Text = ""+threads;
try
{
m_spider.Start(new Uri(this.targetURL.Text),threads);
}
catch( UriFormatException ex)
{
System.Windows.Forms.MessageBox.Show( ex.Message );
return;
}
begin.Text = "Begin";
targetURL.Enabled = true;
threadCount.Enabled = true;
outputDir.Enabled = true;
begin.Enabled = true;
}
}
public void SetLastURL(string str)
{
currentURL.Text = str;
}
public void SetElapsedTime(string str)
{
elapsed.Text = str;
}
public void SetProcessedCount(string str)
{
processedURLs.Text = str;
}
private void SpiderForm_Close(object sender, CancelEventArgs e)
{
Application.Exit();
}
}