Abort thread when regular expression get stuck?
Hello,
I have written an application that takes HTML document and apply predefined regular expression on it to parse certain data from the HTML document.
My problem is sometimes the regular expression get stuck and whole application went to Not responding condition. A solution that I implemented in it to recover from it is by using two thread. The first thread is like a monitor to second thread. In this thread I sleep the thread for 30 secs. It checks after 30 secs. if the 2nd thread has not returned properly or it is still running. If it finds it running, it forcefully abort the 2nd third.
private void thread_timer()
{
try
{
string sleep_interval = ConfigurationManager.ConnectionStrings["sleep_interval"].ToString();
while (true)
{
Thread.Sleep(int.Parse(sleep_interval));
if (pattern_thread.IsAlive == true)
{
pattern_thread.Abort();
LoggingManager.LogWriter.Write("Pattern matching thread is aborted.");
break;
}
else
{
LoggingManager.LogWriter.Write("Pattern Thread is already stopped. Aborting Timer Thread.");
break;
}
}
}
catch (Exception exp)
{
LoggingManager.LogWriter.Write("In thread_timer" + exp.Message);
}
}
In second thread the regular expression pattern matching code is executed.
Regex Reg = new Regex(gstrPattern.ToString(), RegexOptions.IgnoreCase | RegexOptions.Singleline);
MatchCol = Reg.Matches(gDocumentHTML.ToString());
To match the pattern in MatchCol global variable.
But this approach is not working sometimes. The pattern thread still remain suspended and it is not aborted through the first timer thread.
Is there any other solution to fix this issue?
Thanks in advance.
Irfan
|