Asynchronous Regular Expression

Source: Internet
Author: User

Private matchcollection MC; private int _ timeout; // maximum sleep time (timeout), millisecond private int sleepcounter; private int sleepinterval; // sleep interval, millisecond 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, 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, 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 {get {return this. _ RegEx ;}set {This. _ RegEx = value ;}

internal void matchs () {matchcollection MC = This. _ RegEx. Matches (this. _ INPUT); If (MC! = NULL & Mc. Count> 0) // This may cause CPU resource depletion {This. onmatchcomplete (MC) ;}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.