在c#中使用正則表達式進行匹配,有時候我們會遇到這種情況,cpu使用率100%,但是正則表達式并沒有異常拋出,正則一直處于匹配過程中,這將導致系統(tǒng)資源被耗盡,應用程序被卡住,這是由于正則不完全匹配,而且regex中沒有timeout屬性,使正則處理器陷入了死循環(huán)。
這種情況尤其可能發(fā)生在對非可靠的被匹配對象的匹配過程中,例如在我的個人網站www.eahan.com項目中,對多個網站頁面的自動采集匹配,就經常發(fā)生該問題。為了避免資源耗盡的情況發(fā)生,我寫了一個asynchronousregex類,顧名思義,異步的regex。給該類一個設置一個timeout屬性,將regex匹配的動作置于單獨的線程中,asynchronousregex監(jiān)控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);
}
}
}
}
}
新聞熱點
疑難解答