none
why can't abort a newthread with aRegex.Matches in progress? RRS feed

  • Question

  •     try
        { 
         MkMatchSvr mkMatchsvr = new MkMatchSvr(sInputPreParsed, myRegex);
         mkMatchsvr.performMkMatch(TimeSpan.FromMilliseconds(i));
         if (mkMatchsvr.bOut)
          myMatch = mkMatchsvr.myMatch;
         else { setStatus("Failed to find any match. " + mkMatchsvr.sOut); return false; }
        } 
        catch (Exception emm) { setStatus("Input Matched Against Regex failed." + emm.Message); return false; }

     

      private class MkMatchSvr
      {
       private string sInput;
       Regex myRegex;
       protected internal string sOut;
       protected internal bool bOut;
       protected internal MatchCollection myMatch;
       private TimeSpan timeOut;
       public MkMatchSvr(string sInput, Regex aRegex)    // for mkmatch
       {
        this.sInput = sInput;
        this.myRegex = aRegex;
        sOut = "<<NO MATCH - timed out>>";
        bOut = false;
       }
       public void performMkMatch(TimeSpan timeout)
       {
        this.timeOut = timeout;
        System.Threading.Thread newThread = new System.Threading.Thread(work); //makMatch);
        newThread.Start();
        bool bCpt = newThread.Join(timeout);
        if (!bCpt)
         newThread.Abort();
        bIsMkMatchInProgress = false; // 110316 added
       }
       void work() //makMatch()
       {
        try
        {
         myMatch = myRegex.Matches(sInput);
         bOut = true;
         sOut = "";
        } catch (Exception e) { bOut = false; sOut = e.Message; }
       }
      }

    Apparently the applicaiton behave ok when the supplied myRegex with proper no greedy underlying regex.

     However eveythign runs fine if i precede the myRegex.Matches with

    	if (!myRegex.IsMatch(sInput))
    	{
    		bOut = false; 					sOut = "Failed to findMatch";
    		return;
    	}

     

    • Moved by Cookie Luo Tuesday, May 24, 2011 7:12 AM (From:Visual C# General)
    Saturday, May 21, 2011 8:03 PM

Answers

  •  

    Hi,

     

    I think root cause of the issue is that you abort a thread on another thread. See the MSDN document:

    When a thread calls Abort on itself, the effect is similar to throwing an exception; the ThreadAbortException happens immediately, and the result is predictable. However, if one thread calls Abort on another thread, the abort interrupts whatever code is running. There is also a chance that a static constructor could be aborted. In rare cases, this might prevent instances of that class from being created in that application domain. In the .NET Framework versions 1.0 and 1.1, there is a chance the thread could abort while a finally block is running, in which case the finally block is aborted.

     

    According to your description, I think it is not necessary to call Work method on another thread. More information, please refer to: Thread.Abort Method


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Paul Zhou Wednesday, June 1, 2011 5:57 AM
    • Marked as answer by Paul Zhou Tuesday, June 7, 2011 8:01 AM
    Monday, May 30, 2011 8:11 AM

All replies

  • Hi

    Move to Common Language runtime forum for better support.

    Thanks for your understanding.


    Cookie Luo[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, May 24, 2011 7:12 AM
  • Can not exactly understand what your question is.

    What's your question? Any incorrect result? or Errors/Exceptions?


    Hard hard work, Day day up!
    Wednesday, May 25, 2011 8:37 AM
  • allow me trying to clarify the question.

    if a thread started by a time is doing regex.Matches and is not completed when "joined" at timer click,

    it can't be aborted.

     

    however the same thread if still busy wokring on regx.IsMatch. can be aborted with the same code.

    why the difference?

      Note the total cpu consumption is only maximum of 25% and often a lot less, hence it is not the processor not the system too busy to abort


    if the question is still not clear take a look the code with isMatch preceding Matches in http://social.msdn.microsoft.com/Forums/en-AU/regexp/thread/80254692-75b1-405e-8076-78ff4971a28c

    the above code will abort the thread if the ismatch still busy. of couse if ismathces completed but matches still in progress when the timer tick comes along, the thread will not be aborted.

    However the code in the link above does prevent most applicaiton freeze from bad regex. even the bad regex takes too long to complete, double the time increment will work fine provided it has already completed isMatch. I have yet to see bad regex causing trouble otherwise.


    • Edited by fs - ab Friday, June 3, 2011 9:57 PM
    Friday, May 27, 2011 4:58 AM
  •  

    Hi,

     

    I think root cause of the issue is that you abort a thread on another thread. See the MSDN document:

    When a thread calls Abort on itself, the effect is similar to throwing an exception; the ThreadAbortException happens immediately, and the result is predictable. However, if one thread calls Abort on another thread, the abort interrupts whatever code is running. There is also a chance that a static constructor could be aborted. In rare cases, this might prevent instances of that class from being created in that application domain. In the .NET Framework versions 1.0 and 1.1, there is a chance the thread could abort while a finally block is running, in which case the finally block is aborted.

     

    According to your description, I think it is not necessary to call Work method on another thread. More information, please refer to: Thread.Abort Method


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Paul Zhou Wednesday, June 1, 2011 5:57 AM
    • Marked as answer by Paul Zhou Tuesday, June 7, 2011 8:01 AM
    Monday, May 30, 2011 8:11 AM