Threading problems

Hi there. This is wierd

I have a code that works fine, when you abort the background thread it aborts it properly (causes the ThreadAbortException)

however taking the same piece of code and modifying it slightly (not even touching the thread properties) doesnt seem to abort the thread at all!



//Method 1 or something
void SomeMethod()
try
{
...
... do stuff
...
}
catch (ThreadAbortException)
{
   this.theRunningThread = null;
}

private void cmdStart_Click(object sender, System.EventArgs e)
{
   this.theRunningThread = new ThreadStart(SomeMethod);
   this.theRunningThread.IsBackground = true;
   this.theRunningThread.Start();
}

private void cmdStop_Click(object sender, System.EventArgs e)
{
   if (this.theRunningThread != null)
   {
      this.theRunningThread.Abort(System.Threading.ThreadState.Background);
   }
}

 


does this code look ok to you


Answer this question

Threading problems

  • davherb

    sorry my mistake, the code you had posted was originally in my code, i didnt read it properly when i was typing it in....

    it still causes the same problems

  • Jonas

    Abort is bad because mutexes held by aborted thread may not be released properly, causing deadlock to occur.

    The recommended solution is for thread to periodically check a flag to see if it should stop processing and it cleans itself properly by releasing all locks when it has been asked to terminate.


  • Mike81

    Abort is a bad way to kill a thread.. It raises ThreadAbortException.  Programs do not necessarily have to heed it.  It also kills a thread without getting it to a proper stopping point.

    A better way is to create a flag which the function tests and uses to abort gracefully. 

    Shaun Bedingfield
    blogsb.blogspot.com
    shaunbed@houston.rr.com

  • Amer 570

    Your code should be,




    //Method 1 or something
    void SomeMethod()
    try
    {
    ...
    ... do stuff
    ...
    }
    catch (ThreadAbortException)
    {
       this.theRunningThread = null;
    }

    private void cmdStart_Click(object sender, System.EventArgs e)
    {
       this.theRunningThread = new Thread(new ThreadStart(SomeMethod));
       this.theRunningThread.IsBackground = true;
       this.theRunningThread.Start();
    }

    private void cmdStop_Click(object sender, System.EventArgs e)
    {
       if (this.theRunningThread != null)
       {
          this.theRunningThread.Abort(System.Threading.ThreadState.Background);
       }
    }


     


    .Hibri
    www.hibri.net

  • psique

    where/who says aborting a thread is not a clean solution I was always taught and assumed it was - otherwise what other way is there to abort/stop a thread Its built for a reason......

  • Jim815

    Hello,
    I have a similar problem. My thread executes a function running a Directory.GetFiles() that may be take some times, especially if the directory is big and located on remote computer.
    I would like to be able to abort the directory scan if its takes to long.
    If Thread.Abort() is not a 'clean' solution for interrupting the thread, what could I use to get some quite immediate stop for such an expensive function
    Thanks for your answer.


  • Harsh

    well sure if your using locks then yeh of course....

    please provide an example on how you would "release all locks" and clean up the thread when the thread has been finished executing its job to do.... :)

     

    would be useful to myself and others



  • Threading problems