• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 687
  • Last Modified:

Thread.Abort() doesn't kill it

My abort button doesn't work. My code is something like this.

try {
   if (expThread != null) {
           if (expThread.IsAlive) {
              expThread.Abort();
          }
   }
}
catch (Exception Ex)
{
                MessageBox.Show("Error aborting scan: " + Ex.Message);
}


Im developing a Windows application (not Web based) using Visual Studio 2008 and Visual C#

When I press my ABORT button I trace through the code and it always executes expThread.Abort(); but expThread.IsAlive is always true. Shouldn't it be set to false when Abort() is executed?  

An exception is never encountered. ie. The MessageBox is not displayed.

Not sure what is happenning here. Anyone have any ideas?
0
rangers99
Asked:
rangers99
  • 3
  • 2
1 Solution
 
andrewjbCommented:
It;'s asyncronous - use Thread.Join() to wait for the .Abort() to be processsed.

Your thread can also ignore the abort request.
0
 
rangers99Author Commented:
andrew
Thanks. Where does Thread.Join() go in the code?
0
 
andrewjbCommented:
{
  expThread.Abort();
  expThread.Join();
}
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
CebikCommented:
and it's better to use

catch (ThreadAbortException)
{
      return;
}

Open in new window

0
 
rangers99Author Commented:
Cebik , yes its better but its not really the issue here.

andrew
I tried thread.Join() but my application just hangs. My thread appears to be un-abortable. Im not sure what the hell is going on.
0
 
andrewjbCommented:
What's your thread doing? .Abort() raises a ThreadAbortException on the thread. If the thread catches+ignores that, it'll never stop. Or if it is itself stuck waiting for a signal or mutex, then it'll remain waiting.
I can't tell what the issue is without seeing what the thread is doing.
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now