关闭

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

500人阅读 评论(0) 收藏 举报

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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:88205次
    • 积分:1724
    • 等级:
    • 排名:千里之外
    • 原创:41篇
    • 转载:116篇
    • 译文:0篇
    • 评论:6条
    文章分类
    最新评论