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?

[Webinar] Streamline your web hosting managementRegister Today

x
 
jef06Connect With a Mentor Commented:
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
All Courses

From novice to tech pro — start learning today.