How to kill thread when regular expression get stuck?

Posted on 2007-03-21
Last Modified: 2013-12-17

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.
Question by:bullseye_pak
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment

Accepted Solution

jef06 earned 500 total points
ID: 18769567
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;

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

                    // the thread is not running, so free resources
                return false;

        // Signal thread to stop work
        public void SignalToStop()
            // stop thread
            if (thread != null)
                // signal to stop

        // Wait for thread stop
        public void WaitForStop()
            if (thread != null)
                // wait for thread stop


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

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

            // release events
            stopEvent = null;

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question