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