Solved

How to kill thread when regular expression get stuck?

Posted on 2007-03-21
1
369 Views
Last Modified: 2013-12-17
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
0
Comment
Question by:bullseye_pak
1 Comment
 
LVL 5

Accepted Solution

by:
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;
                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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…

821 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