网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > C#应用
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.关于C#中的DateTime类型的细节问.
.在Visual C#.Net中使用CWGraph控.
.C#图像放大问题解决方法.
.c#实现google样式的分页.
.C#一个象棋游戏程序.
.利用C#实现标注式消息提示窗口.
.浅析.NET开发中代理模式的使用.
.使用c#捕获windows的关机事件.
.列出C#进程以及详细信息.
.解读C#中的正则表达式.
.在C#中使用COM+实现事务控制.
.C#中利用Markup Service实现HTML.
.c#中获取存储过程方法.
.c# 实现Word联接Excel的MailMerg.
.C#中的函数重载.
.在C#中应用哈希表(Hashtable) .
.C#中接口的深入浅出.
.用Visual C#获得计算机名称和IP.
.C#的Windows编程中多语言的实现.
.js也可以有自定义事件 注入就是这.

关于正则表达式匹配无异常资源耗尽的解决方案

发表日期:2007-4-25


        在c#中使用正则表达式进行匹配,有时候我们会遇到这种情况,cpu使用率100%,但是正则表达式并没有异常抛出,正则一直处于匹配过程中,这将导致系统资源被耗尽,应用程序被卡住,这是由于正则不完全匹配,而且Regex中没有Timeout属性,使正则处理器陷入了死循环。
        这种情况尤其可能发生在对非可靠的被匹配对象的匹配过程中,例如在我的个人网站www.eahan.com项目中,对多个网站页面的自动采集匹配,就经常发生该问题。为了避免资源耗尽的情况发生,我写了一个AsynchronousRegex类,顾名思义,异步的Regex。给该类一个设置一个Timeout属性,将Regex匹配的动作置于单独的线程中,AsynchronousRegex监控Regex匹配超过Timeout限定时销毁线程。


using System;

using System.Text.RegularExpressions;
using System.Threading;

namespace LZT.Eahan.Common
{
    public class AsynchronousRegex
    {
        private MatchCollection mc;
        private int _timeout;        // 最长休眠时间(超时),毫秒
        private int sleepCounter;
        private int sleepInterval;    // 休眠间隔,毫秒
        private bool _isTimeout;

        public bool IsTimeout
        {
            get {return this._isTimeout;}
        }

        public AsynchronousRegex(int timeout)
        {
            this._timeout = timeout;
            this.sleepCounter = 0;
            this.sleepInterval = 100;
            this._isTimeout = false;

            this.mc = null;
        }

        public MatchCollection Matchs(Regex regex, string input)
        {
            Reg r = new Reg(regex, input);
            r.OnMatchComplete += new Reg.MatchCompleteHandler(this.MatchCompleteHandler);
           
            Thread t = new Thread(new ThreadStart(r.Matchs));
            t.Start();

            this.Sleep(t);

            t = null;
            return mc;
        }

        private void Sleep(Thread t)
        {
            if (t != null && t.IsAlive)
            {
                Thread.Sleep(TimeSpan.FromMilliseconds(this.sleepInterval));
                this.sleepCounter ++;
                if (this.sleepCounter * this.sleepInterval >= this._timeout)
                {
                    t.Abort();
                    this._isTimeout = true;
                }
                else
                {
                    this.Sleep(t);
                }
            }
        }

        private void MatchCompleteHandler(MatchCollection mc)
        {
            this.mc = mc;
        }

        class Reg
        {
            internal delegate void MatchCompleteHandler(MatchCollection mc);
            internal event MatchCompleteHandler OnMatchComplete;

            public Reg(Regex regex, string input)
            {
                this._regex = regex;
                this._input = input;
            }

            private string _input;
            public string Input
            {
                get {return this._input;}
                set {this._input = value;}
            }

            private Regex _regex;
            public Regex Regex
            {
                get {return this._regex;}
                set {this._regex = value;}
            }

            internal void Matchs()
            {
                MatchCollection mc  = this._regex.Matches(this._input);
                if (mc != null && mc.Count > 0)    // 这里有可能造成cpu资源耗尽
                {
                    this.OnMatchComplete(mc);
                }
            }
        }
    }
}

上一篇:使用c#捕获windows的关机事件 人气:5190
下一篇:C#分析数据库结构,使用XSL模板自动生成代码 人气:5408
浏览全部正则表达式的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐