All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
.Net,2.0,原汁原味读取注册表_[Asp.Net教程]
ASP.NET日历控件二次开发和使用_[Asp.Net教程]
PHP中的正则表达式_[PHP教程]
用Php模拟asp.net的页面模型_php资料_编程技术
当ASP.NET撞上JSF之模板化、式样和主题_[Asp.Net教程]
WML学习(六):事件_[XML教程]
ASP.NET,2.0移动开发之定义设备筛选器_.net资料_编程技术
PHP数组的定义、初始化和数组元素的显示_php资料_编程技术
javascript事件描述_JavaScript技术_编程技术
SQL里的cast和convert_[SQL,Server教程]
Reading number is top 10 pictures
做运动的校花2
HongMenYan premiere XinLiangGong clairvoyant outfit PK YiFeiLiu1
程序员的悲哀
超级大兔子
The money of more than 100 countries and regions19
So beauty, will let you spray blood8
Ashlynn Brooke show proud chest measurement3
Other people's teacher VS my teacher
Players in the eyes of a perfect love1
Japanese snow monkeys in the hot spring to keep warm, close their eyes to enjoy
Download software ranking
WebService在.NET中的实战应用教学视频 → 第4集
Tram sex maniac 2 (H) rar bag11
艳兽都市
Tram sex maniac 2 (H) rar bag17
打鸟视频
Wild things 2
C++编程教程第三版
Tram sex maniac 2 (H) rar bag2
dreamweaver8中文版
The king of fighters 97(Mobile phone games-apk)
aaa published in(发表于) 2013/12/15 8:42:21 Edit(编辑)
C#关于正则表达式匹配无异常资源耗尽的解决方案_.net资料_编程技术

C#关于正则表达式匹配无异常资源耗尽的解决方案_.net资料_编程技术

C#关于正则表达式匹配无异常资源耗尽的解决方案_.net资料_编程技术-你的首页-uuhomepage.com

  在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);
}
}
}
}
}





添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.