How to kill thread when regular expression get stuck?

Hello,

I have written an application that crawl HTML documents and apply pre-defined regular expression on them (I can't change regular expressions). The application starts the crawling and apply these patterns and store the parsed results in DB.
Now the problem comes when a regular expression is applied on doc it get stuck and whole application goes to Not Responding. The application is a windows service and we don't monitor it all the time.
What I did to solve it by adding the regular expression matching code into another thread. There is another thread that monitor this regex thread if it get stuck for more than 30 secs. Then it Abort that regex.
Sometimes this approach works and most of the time the application again goes into Not Responding state. After analyzing the code I come to this point that the MatchCollection object that is passed into the regex thread when returns display this message in the debugger "Function evaluation disabled because a previous function evaluation timed out.... "
When we try to access any method or attribute of MatchCol object the application goes to Not Responding.

What should be the better work around for this? What should be the proper way to get the MatchCollection object from the regex thread? Should this object defined as global or it should be passed as reference? Should this object be returned from the thread?
What is the best way to tackle regex in threads?

I look forward to get a solution for this problem.
Thanks,
Irfan
bullseye_pakAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jef06Commented:
You might get a an exception in that thread that you are not catching and because on it is on a different thread it does not crash your app. Could you try to add Application.ThreadException in the constructor of service window and see if you get one, this should collect all the exception.


Try the following code:
// Instanctiate the object MyThread and call Start method to start it.
public MyThread
    {
        private Thread thread = null;
        private ManualResetEvent stopEvent = null;

        private void StartRegex()
        {
            while (!stopEvent.WaitOne(0, true))
            {
             //here add you code
            }
        }

        // Start work
        public void Start()
        {
            if (thread == null)
            {
                // create events
                stopEvent = new ManualResetEvent(false);

                // create and start new thread
                thread = new Thread(new ThreadStart(StartRegex));
                thread.Priority = ThreadPriority.Normal;
                thread.Start();
            }
        }

        // Get state of the video source thread
        public bool Running
        {
            get
            {
                if (thread != null)
                {
                    if (thread.Join(0) == false)
                        return true;

                    // the thread is not running, so free resources
                    Free();
                }
                return false;
            }
        }

        // Signal thread to stop work
        public void SignalToStop()
        {
            // stop thread
            if (thread != null)
            {
                // signal to stop
                stopEvent.Set();
            }
        }

        // Wait for thread stop
        public void WaitForStop()
        {
            if (thread != null)
            {
                // wait for thread stop
                thread.Join();

                Free();
            }
        }

        // Abort thread
        public void Stop()
        {
            if (this.Running)
            {
                thread.Abort();
            }
        }

        // Free resources
        private void Free()
        {
            thread = null;

            // release events
            stopEvent.Close();
            stopEvent = null;
        }
    }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.