Solved

Thread.Abort() doesn't kill it

Posted on 2009-05-07
6
665 Views
Last Modified: 2012-05-06
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
Comment
Question by:rangers99
  • 3
  • 2
6 Comments
 
LVL 12

Expert Comment

by:andrewjb
ID: 24324116
It;'s asyncronous - use Thread.Join() to wait for the .Abort() to be processsed.

Your thread can also ignore the abort request.
0
 

Author Comment

by:rangers99
ID: 24324135
andrew
Thanks. Where does Thread.Join() go in the code?
0
 
LVL 12

Accepted Solution

by:
andrewjb earned 100 total points
ID: 24324191
{
  expThread.Abort();
  expThread.Join();
}
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 6

Expert Comment

by:Cebik
ID: 24324194
and it's better to use

catch (ThreadAbortException)

{

      return;

}

Open in new window

0
 

Author Comment

by:rangers99
ID: 24324794
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
 
LVL 12

Expert Comment

by:andrewjb
ID: 24324841
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now