在c#中使用正則表達(dá)式進(jìn)行匹配,有時(shí)候我們會(huì)遇到這種情況,cpu使用率100%,但是正則表達(dá)式并沒有異常拋出,正則一直處于匹配過程中,這將導(dǎo)致系統(tǒng)資源被耗盡,應(yīng)用程序被卡住,這是由于正則不完全匹配,而且Regex中沒有Timeout屬性,使正則處理器陷入了死循環(huán)。
這種情況尤其可能發(fā)生在對(duì)非可靠的被匹配對(duì)象的匹配過程中,例如在我的個(gè)人網(wǎng)站www.eahan.com項(xiàng)目中,對(duì)多個(gè)網(wǎng)站頁面的自動(dòng)采集匹配,就經(jīng)常發(fā)生該問題。為了避免資源耗盡的情況發(fā)生,我寫了一個(gè)AsynchronousRegex類,顧名思義,異步的Regex。給該類一個(gè)設(shè)置一個(gè)Timeout屬性,將Regex匹配的動(dòng)作置于單獨(dú)的線程中,AsynchronousRegex監(jiān)控Regex匹配超過Timeout限定時(shí)銷毀線程。
using System;
using System.Text.RegularExPRessions;
using System.Threading;
namespace LZT.Eahan.Common
{
public class AsynchronousRegex
{
private MatchCollection mc;
private int _timeout; // 最長(zhǎng)休眠時(shí)間(超時(shí)),毫秒
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);
}
}
}
}
}
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注