|
-
Jun 6th, 2004, 08:02 PM
#1
Thread Starter
New Member
Threading problem using AsyncDelegate
Background info:
I have a function which uses regular expressions to validate and parse data. When i submit invalid data to this function it sometimes hangs while trying to execute the regular expression IsMatch method, no error messages or exceptions are thrown, it just freezes on that statement.
I have been advised to execute this function in another thread, so that i can abort the thread when it freezes.
I have attempted to implement this through an AsyncDelegate
Code Snippet:
'strentry is passed into this function as parameter - string to be validated
Function CompareReg(ByVal regrecord As Regex, ByVal strLookup As String) As Boolean
CompareReg = regrecord.IsMatch(strLookup)
End Function
Delegate Function myMethodDelegate(ByVal regrecord As Regex, ByVal strLookup As String) As Boolean
Dim strPattern As String
strPattern = "6\D*(\d+)[^\$]*\$([\d,.]*)" ''0,1,2
strPattern += "[^+]*[^\d]*([\d,]+)[^\$]*\$([\d,.]*)" '3,4
strPattern += "[^5]*5[^\d]*([\d,]+)[^\$]*\$([\d,.]*)" '5,6
strPattern += "[^4]*4[^\d]*([\d,]+)[^\$]*\$([\d,.]*)" '7,8
strPattern += "[^\$]*\$([\d,.]*)" '9
strPattern += "[^3]*3[^\d]*([\d,]+)[^\$]*\$([\d,.*])" '10,11
Dim regRecord As New Regex(strPattern)
Dim dlgt As New AsyncDelegate(AddressOf CompareReg)
Dim ar As IAsyncResult = dlgt.BeginInvoke(regRecord, strEntry, Nothing, Nothing)
Dim bWait As Boolean = ar.AsyncWaitHandle.WaitOne(2000, True)
If ar.IsCompleted Then
isMatched = dlgt.EndInvoke(ar)
Else
ar.AsyncWaitHandle.Close()
dlgt = Nothing
End If
the compareReg function attempts to execute the regular expression IsMatch statement
this solution works but has a downside - the delegate creates a new thread but does not abort it when i remove the delegate. So when the tread freezes and the timeout has been reached, after i execute the statements in the Else block, I can still see the thread running in the debugger.
This program is intended to validate thousands of records, and I cant have a new thread being created and left in memory even for the very small percentage of records that will cause this type of freeze.
My question is: is there any way to get the thread ID created by the AsyncDelegate so that i could try aborting it manually. Or some other way i could try aborting it.
I was hoping i wouldnt have to mess with the Monitor class to run one statement asynchrounsly...
Second question is: Does anyone know why the regex would freeze like that?
execution of the IsMatch freezes when the the validating record in strEntry is missing the 9th capture group in the pattern
the pattern is meant to look for 5 pairs of money values which starts with a $ and a double value. + one seperate double value to be captured in the 9th group. With a bunch of html code between the values. It works fine for all valid records allowing me extract the group matches after running the match method later in the function.
Any code/advice would be greatly appreciated.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|