国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 海晏县| 乳山市| 瓦房店市| 含山县| 徐水县| 禹城市| 古浪县| 广东省| 拉萨市| 定西市| 冀州市| 翁牛特旗| 二连浩特市| 香河县| 泸定县| 佛山市| 滁州市| 木兰县| 静海县| 宁津县| 邯郸市| 徐汇区| 元阳县| 如东县| 丽水市| 宝兴县| 南皮县| 滨海县| 潢川县| 贵阳市| 南陵县| 通榆县| 鄱阳县| 逊克县| 常山县| 望都县| 栖霞市| 洛扎县| 辽源市| 修文县| 广西|