Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 684
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

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