欢迎光临
我们一直在努力

建站超值云服务器,限时71元/月

using system;
using system.threading;
using system.net;
using system.text;
using system.text.regularexpressions;
using system.io;

namespace threadpooldelegate
{
    //public delegate void receivecompletedeventhandler(string url,string title,string sourcecode);
    public delegate void receivecompletedeventhandler(object sender,receivecompletedeventargs e);
    
    /// <summary>
    /// class1 的摘要说明。
    /// </summary>
    public class class1
    {        
        public event receivecompletedeventhandler receivecompletedevent;

        public class1()
        {
            //
            // todo: 在此处添加构造函数逻辑
            //
        }

        public void readsourcecode(object o)
        {

            string url = (string)o;
            string title = "";
            string sourcecode = "";

            webclient wc = new webclient();

            byte[] data = wc.downloaddata(url);

            sourcecode = encoding.default.getstring(data);

            title = this.getpagetitle(sourcecode);

            if (receivecompletedevent != null)
            {
                //receivecompletedevent(url,title,sourcecode);
                receivecompletedevent(this,new receivecompletedeventargs(url,title,sourcecode));
            }
        }

        public void readsourcecode(object o,bool timeout)
        {

            string url = (string)o;
            string title = "";
            string sourcecode = "";

            webclient wc = new webclient();

            byte[] data = wc.downloaddata(url);

            sourcecode = encoding.default.getstring(data);

            title = this.getpagetitle(sourcecode);

            if (receivecompletedevent != null)
            {
                //receivecompletedevent(url,title,sourcecode);
                receivecompletedevent(this,new receivecompletedeventargs(url,title,sourcecode));
            }
        }

        private string getpagetitle(string str)
        {
            string ltitle = "";
            string lpattern = "(?:<\\s*title\\s*>(?<t>[^<]+))";
            regex rx = new regex(lpattern, regexoptions.ignorecase | regexoptions.compiled );
            match mt = rx.match(str);
            if ( mt.success )
                try
                {
                    ltitle = mt.groups["t"].value.tostring();
                }
                catch
                {
                    ltitle ="";
                }
            else
                ltitle = "";
            return ltitle;
        }

        
    }

    public class test
    {
        static manualresetevent ev = new manualresetevent(false);

        private static int intcount;

        private static datetime dtstart = datetime.now;

        public static void main(string[] args)
        {
        
            class1 c = new class1();
            c.receivecompletedevent += new receivecompletedeventhandler(writeout);

            
            if (args.length >=1)
            {
                for (int i=0;i<args.length;i++)
                {
                    //threadpool.queueuserworkitem(new waitcallback(c.readsourcecode),args[i]);
                    
                    threadpool.registerwaitforsingleobject(ev,new waitortimercallback(c.readsourcecode),args[i],3000,true);

                    intcount++;
                }

                ev.waitone();
            }
            else
            {
                console.writeline("you must pass an argument at least ");
            }
            
        }

        
        private static void writeout(object sender,receivecompletedeventargs e)
        {
            try
            {
                intcount–;

                console.writeline("正在执行……{0}",e.url);

                filestream fs = new filestream(@"c:\"+e.title+".txt",filemode.create,fileaccess.write);
            
                streamwriter sw = new streamwriter(fs);

                sw.writeline(e.url);
                sw.writeline();
                sw.writeline(e.title);
                sw.writeline();
                sw.writeline(e.sourcecode);
                sw.writeline();

                sw.flush();
            
                fs.flush();

                sw.close();

                fs.close();

                console.writeline("执行完成……{0}",e.url);

                if(intcount == 0)
                {
                    console.writeline("程序执行完成…..");

                    timespan tsspent = datetime.now.subtract(dtstart);

                    console.writeline("程序执行时间……{0}毫秒",tsspent.totalmilliseconds);

                    console.writeline("程序在退出之前等待5秒,only for demo");

                    thread.sleep(5000);

                    ev.set();
                }
            }
            catch(exception ex)
            {
                throw new exception(ex.message);
            }

        }
        

        /*
        private static void writeout(string url,string title,string sourcecode)
        {
            intcount–;
        
            filestream fs = new filestream(@"c:\"+title+".txt",filemode.create,fileaccess.write);

            streamwriter sw = new streamwriter(fs);

            sw.writeline(url);
            sw.writeline();
            sw.writeline(title);
            sw.writeline();
            sw.writeline(sourcecode);
            sw.writeline();

            sw.flush();
            fs.flush();

            sw.close();

            fs.close();
            
            console.writeline("执行完成……{0}",e.url);

            if(intcount == 0)
            {
                console.writeline("程序执行完成…..");

                timespan tsspent = datetime.now.subtract(dtstart);

                console.writeline("程序执行时间……{0}毫秒",tsspent.totalmilliseconds);

                console.writeline("程序在退出之前等待10秒,only for demo");

                thread.sleep(10000);

                ev.set();
            }

        }
        */
        
    
    }

    public class receivecompletedeventargs : system.eventargs
    {
        string _url;
        string _title;
        string _sourcecode;

        public string url
        {
            get
            {
                return _url;
            }

        }

        public string title
        {
            get
            {
                return _title;
            }
        }

        public string sourcecode
        {
            get
            {
                return _sourcecode;
            }
        }

        public receivecompletedeventargs(string url,string title,string sourcecode)
        {
            this._url = url;
            this._title = title;
            this._sourcecode = sourcecode;
        }
    }
}

这里定义了两种委托的方式,大家随便看看

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 »
分享到: 更多 (0)

相关推荐

  • 暂无文章