国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

C#關于正則表達式匹配無異常資源耗盡的解決方案

2024-07-21 02:28:14
字體:
來源:轉載
供稿:網友


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

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 莎车县| 建德市| 江口县| 岳普湖县| 瑞昌市| 昌邑市| 宜昌市| 咸宁市| 达日县| 荃湾区| 黑水县| 朝阳市| 永福县| 西林县| 响水县| 安远县| 阳谷县| 赤城县| 凌源市| 吐鲁番市| 苗栗县| 琼海市| 如皋市| 布尔津县| 大英县| 高安市| 东辽县| 萨嘎县| 潞西市| 呼玛县| 青海省| 邢台市| 通化市| 北宁市| 辉南县| 鄄城县| 高青县| 吉安市| 屏山县| 麻江县| 师宗县|