2

Possible Duplicate:
Implementing RegEx Timeout in .NET 4

Regex regexpr = new Regex(anchorPattern[item.Key], RegexOptions.Singleline, TimeSpan.FromMilliseconds(10));

"System.Text.RegulerExpression.Regex" does not contain a constructer that takes 3 arguments. Note :The error is in the framework 4. if you use the framework 4.5 you won't encounter this error. But i have been using framework 4 and I have to set timeout regexpr. What is the remedy to this ?

Community
  • 1
  • 1
RockOnGom
  • 3,893
  • 6
  • 35
  • 53
  • 1
    Take a look at [Implementing RegEx Timeout in .NET 4](http://stackoverflow.com/questions/9460661/implementing-regex-timeout-in-net-4) - this has an example of how to implement this yourself. (NB It doesn't let you create a `Regex` that inherantly times out, but it lets you time out individual calls to `Match` etc.) – Rawling Nov 23 '12 at 09:47

1 Answers1

3

There is no such constructor as the one you are using in .NET 4. Take a look at documentation page; the only options for constructor are:

Regex()

Regex(String)

Regex(SerializationInfo, StreamingContext)

Regex(String, RegexOptions)

EDIT

You can use a Task to run the regex and Wait method to pass the timeout. Something like this should do the work:

var regexpr = new Regex(anchorPattern[item.Key], RegexOptions.Singleline);
var task = Task.Factory.StartNew(()=>regexpr.Match(foo));
var completedWithinAllotedTime = task.Wait(TimeSpan.FromMilliseconds(10));
Community
  • 1
  • 1
RePierre
  • 9,358
  • 2
  • 20
  • 37
  • ı know the regex constructer in fw 4.0. I have asking been how to set the timeout to regex in framework 4. 0 . – RockOnGom Nov 23 '12 at 10:01
  • @alikoyuncu, Please see the latest edit. – RePierre Nov 23 '12 at 10:24
  • While the task.wait works perfectly, it leaves the regex running in the background. To end it, call task.Wait(new System.Threading.CancelationToken(true)); after the first wait. Catch the exception and put your timeout processing in there. – ChopperCharles May 22 '15 at 15:57