c# 线程池 后台目录扫描器

http://apps.hi.baidu.com/share/detail/30009868

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Threading;
using System.Net;
using System.Windows.Forms.PropertyGridInternal;

namespace scan
{
public partial class Form1 : Form
{

public Form1()
{
InitializeComponent();
}
public delegate void functionDele();
public functionDele doEvent;

public delegate void functionDele1(string s);
public functionDele1 doEvent1;

bool tag = true;//是否停止,true为否
private void button1_Click(object sender, EventArgs e)
{
tag = true;
this.progressBar1.Value = 0;
this.listBox1.Items.Clear();
if (this.textBox1.Text.Trim() == "")
{
MessageBox.Show("先列举可能的后台目录");
return;
}
if (this.textBox3.Text.Trim() == ""||this.textBox3.Text.Trim().ToLower()=="http://")
{
MessageBox.Show("输入网址不正确");
return;
}
string[] strarr = this.textBox1.Text.Trim().Split(new string[]{ "\r\n" }, StringSplitOptions.None);//以回车分割成string数组
WaitCallback waitcallback = new WaitCallback(DoWork);
int count = strarr.Length;
//Console.WriteLine(count); //用console.writeline是为了调试时候用view->output来查看的
progressBar1.Maximum = count;
ThreadPool.SetMaxThreads(30, 100);//最大线程数 超过30自动排队(threadpool自动管理)
ThreadPool.SetMinThreads(20, 100);
for (int i = 0; i < strarr.Length; i++)
{
ThreadPool.QueueUserWorkItem(waitcallback,strarr[i]);//把任务不断压入线程池;线程池自动创建若干个线程循环做事
}
}
public void DoWork(object state)
{
if(!tag)//停止
return;
HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create(this.textBox3.Text.Trim()+"/" + state.ToString() + "/");
hwr.AllowAutoRedirect = false; //不允许重定向
hwr.Timeout = 3000; //连接超时时间设置
hwr.Method = "GET"; //协议:GET、HEAD、POST、PUT、DELETE、TRACE 或 OPTIONS。
try
{
HttpWebResponse hwrs = (HttpWebResponse)hwr.GetResponse();
if (hwrs.StatusCode == HttpStatusCode.OK) //获得http状态码 如:200但是404却捕捉不到
{
//this.listBox1.Items.Add(this.textBox3.Text.Trim() + "/" + state.ToString() + "/");
if (doEvent1 != null)
{
this.BeginInvoke(doEvent1,new object[]{this.textBox3.Text.Trim() + "/" + state.ToString() + "/"});//
//过代理执行listbox项增加的方法,用的是ui线程的异步方法来执行(this)
}
}
}
catch (Exception ex)
{
//MessageBox.Show(ex.ToString());
if (ex.ToString().ToLower().IndexOf("403") != -1)//403禁止列目录 说明存在
{
if (doEvent1 != null)
{
this.BeginInvoke(doEvent1, new object[] { this.textBox3.Text.Trim() + "/" + state.ToString() + "/" });//通
//过代理执行listbox项增加的方法,用的是ui线程的异步方法来执行(this)
}
}
}
if (doEvent != null)
{
Thread.Sleep(1000);
this.BeginInvoke(doEvent);//通过代理执行进度条值++的方法,用ui线程的异步方法执行该方法
}
//Console.WriteLine(this.progressBar1.Value);
}

private void Form1_Load(object sender, EventArgs e)
{
//Control.CheckForIllegalCrossThreadCalls = false; //线程间相互给控件属性赋值会冲突 ,用这个方法或用代理解决
//textBox3.Click += new System.EventHandler(textBox3_Enter);//鼠标进入textbox触发该事件,执行选中所有文字的方法
doEvent=new functionDele(addProgressBarvalue);//代理要执行的方法(progress进度条前进)
doEvent1 = new functionDele1(addListBoxItem);//代理要执行的方法(listbox项增加)
}
private void button2_Click(object sender, EventArgs e)
{//stop
//this.progressBar1.Value = 0;
tag = false;
}

private void textBox3_Enter(object sender, EventArgs e)
{
//textBox3.SelectAll(); //选中textbox的所有文字

}
void addProgressBarvalue()//进度条值++;
{
this.progressBar1.Value += 1;
}
void addListBoxItem(string s)//listbox项增加
{
this.listBox1.Items.Add(s);
}

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
//Clipboard.SetDataObject(this.listBox1.SelectedItem.ToString());//设置剪切板内容
//MessageBox.Show(Clipboard.GetDataObject().GetData(DataFormats.Text,true).ToString());//获得剪切板内容
System.Diagnostics.Process.Start("iexplore.exe", this.listBox1.SelectedItem.ToString());//ie打开该网址
}
catch { }
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值